Python doesn't have any built-in support for 8 or 16-bit integers. NumPy, on the other hand, does support the sizes you're looking for:

import numpy as np
print(np.uint8(22))
Answer from BrokenBenchmark on Stack Overflow
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.01-understanding-data-types.html
Understanding Data Types in Python | Python Data Science Handbook
np.zeros(10, dtype='int16') Or using the associated NumPy object: np.zeros(10, dtype=np.int16) More advanced type specification is possible, such as specifying big or little endian numbers; for more information, refer to the NumPy documentation. NumPy also supports compound data types, which will be covered in Structured Data: NumPy's Structured Arrays.
Discussions

python - what is the difference between int16 and in32 dtype - Stack Overflow
3 why operations with dtype np.int64 are much slower compared to same operations with np.int16? 1 Interpreting numpy.int64 datatype as native int datatype in Python on windows x64 More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - How to convert int32 numpy array into int16 numpy array? - Stack Overflow
I want to conert a numpy array from int32 type to int16 type. I have an int32 array called array_int32 and I am converting that to int16. import numpy as np array_int32 = np.array([31784960, 690... More on stackoverflow.com
🌐 stackoverflow.com
c - Convert python int into int16_t type - Stack Overflow
I know that python int can be converted into an c int type using ctypes. But how do I convert a python int into an int16_t type? I have tried: import ctypes as ct my_c_number = ct.c_int16_t(1) # More on stackoverflow.com
🌐 stackoverflow.com
What are some real world applications of having int8 , int16, int 32 etc ?
Oh boy.... What were you doing before going into JavaScript? More on reddit.com
🌐 r/golang
28
3
September 27, 2022
🌐
Reddit
reddit.com › r/learnpython › declaring an unsigned integer 16 in python for bit shift operation
r/learnpython on Reddit: Declaring an Unsigned Integer 16 in Python for Bit Shift Operation
October 29, 2023 -

SOLVED: 3 Solutions:

  1. using Numpy : np.uint16()

  2. Using CTypes : ctypes.c_uint16()

  3. Using Bitwise : & 0xFFFF


Hi, I'm trying to convert this code to Python from Go / C. It involves declaring a UInt16 variable and run a bit shift operation. However cant seem to create a variable with this specific type. Need some advise here.

Go Code:

package main

import "fmt"

func main() {

var dx uint16 = 38629

var dy uint16 = dx << 8

fmt.Println(dy) //58624 -> Correct value

}

Python Code:

dx = 38629

dy = (dx << 8)

print(dy) # 9889024 -> not the expected value

print(type(dx)) # <class 'int'>

print(type(dy)) # <class 'int'>

I cant seem to figure out a way to cast or similar function to get this into an Unsigned Int 16.\

Please help.

🌐
Readthedocs
iec104-python.readthedocs.io › latest › python › number › int16.html
Signed Integer (16-bit) — iec104-python 2.2 documentation
c104 python module · Number · Signed Integer (16-bit) View page source · class c104.Int16 · __init__(self, value: int) → None · create a fixed-length integer instance · Parameters: value (int) – the value · Raises: ValueError – cannot convert value to fixed-length integer ·
Top answer
1 of 2
3

It is the number of bits used to represent each integer, as you say, but consider what happens when you try to print the 10 int16 words as int32 words. Each int32 word will be built from two int16 words. Thus there will be only 5 int32words as you see.

Further, the byte order in 32 bit integers is so that the first 16 bit word will be the LSB bits. Thus looking at the hex representation of your two first words. In 16 bits they are: 0x0000 = 0 and 0x0001 = 1. When the same bits are used in a 32 bit word, you get 0x0001 0000 = 65536 due to this somewhat unintuitive bit order.

I hope this helps your understanding of the concepts.

2 of 2
3

Ref: Array types in Numpy and as per the ndarray.view doc

a.view(dtype=some_dtype) constructs a view of the array’s memory with a different data-type. This can cause a reinterpretation of the bytes of memory.

  1. Consider b = np.arange(10, dtype = 'int16')
    It generates an evenly spaced array from 0 to 9. [0 1 2 3 4 5 6 7 8 9]

    1.1 Viewing this array as int32 merges the array by (32/16) = 2.
    Also, trying to do this operation on an odd length array will result in error.

    The output is [ 65536 196610 327684 458758 589832]
    Explanation: [ 65536 * 1 + 0, 65536 * 3 + 2, 65536 * 5 + 4, 65536 * 7 + 6, 65536 * 9 + 8]

  2. Consider b = np.arange(10, dtype = 'int32')
    It is equalivalent to np.arange(10) which simply creates an evenly spaced array from 0 to 9.

    2.1 Viewing this data as int16 splits each element in the data into (32/16) = 2 parts.
    Therefore, a = b.view(dtype = 'int16') is [0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0]

    2.2 Viewing this data as int8 splits each element in the data into (32/8) = 4 parts.
    Therefore, a = b.view(dtype = 'int8') is [0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 6 0 0 0 7 0 0 0 8 0 0 0 9 0 0 0]

Find elsewhere
🌐
Snyk
snyk.io › advisor › nptyping › functions › nptyping.int16
How to use the nptyping.Int16 function in nptyping | Snyk
def test_bits(self): self.assertEqual(8, Int8.bits()) self.assertEqual(16, Int16.bits()) self.assertEqual(32, Int32.bits()) self.assertEqual(64, Int64.bits()) self.assertEqual(16, Float16.bits()) self.assertEqual(32, Float32.bits()) self.assertEqual(64, Float64.bits())
🌐
GitHub
gist.github.com › 711360947b40e8cc404e
Convert int to 16 bits in python · GitHub
Convert int to 16 bits in python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Polars
docs.pola.rs › docs › python › dev › reference › api › polars.datatypes.Int16.html
polars.datatypes.Int16 — Polars documentation
class polars.datatypes.Int16[source]# 16-bit signed integer type. __init__(*args, **kwargs)# Methods ·
🌐
Data-apis
data-apis.org › array-api › latest › API_specification › data_types.html
Data Types — Python array API standard 2025.12 documentation
The default integer data type should be the same across platforms, but the default may vary depending on whether Python is 32-bit or 64-bit.
🌐
Reddit
reddit.com › r/golang › what are some real world applications of having int8 , int16, int 32 etc ?
What are some real world applications of having int8 , int16, int 32 etc ? : r/golang
September 27, 2022 - 353K subscribers in the golang community. Ask questions and post articles about the Go programming language and related tools, events etc.
🌐
Runebook
runebook.dev › english › articles › python
Python - ctypes.c_int16 vs. numpy.int16: Choosing the Right 16-bit Integer Type
Make sure it matches what you're doing in your Python code. Minimal Example Create a small, self-contained example that reproduces the problem. This will make it easier to isolate the issue and get help if you need it. Check for Errors Pay close attention to any error messages you get. They often provide valuable clues about the problem. import ctypes # Create a c_int16 object my_int = ctypes.c_int16() # Assign a value my_int.value = 12345 print(f"Value: {my_int.value}") # Output: Value: 12345 # Assign a negative value my_int.value = -5000 print(f"Value: {my_int.value}") # Output: Value: -5000 # Demonstrate overflow my_int.value = 40000 # Value exceeds the maximum for c_int16 print(f"Value (overflow): {my_int.value}") # Output will be something unexpected my_int.value = -40000 # Underflow!