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

Declaring an Unsigned Integer 16 in Python for Bit Shift Operation
I would just do this: dy = (dx << 8) & 0xFFFF The 0xFFFF is 16 1's, so when you do a bitwise AND everything above the 16th bit is set to 0. More on reddit.com
🌐 r/learnpython
10
1
October 29, 2023
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
BUG: uint16 inserted as int16 when assigning row with dict
This also happens when assigning into an existing object-typed column (the conversion sequence seems to be -> int16 -> int in that case). It's expected the dtype is preserved - uint16 if possible, or an int which is large enough to represent the value. python : 3.8.10.final.0 python-bits : ... More on github.com
🌐 github.com
8
June 9, 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]

🌐
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())
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 7797 › y-do-we-have-int-int16-int32-and-int64
y do we have int, int16, int32, and int64 | Sololearn: Learn to code for FREE!
They all differ in the MAXIMUM VALUE that they,can hold. like, Int32 can hold bigger number that Int16. Hope this is what you need. 23rd Jun 2016, 4:07 PM · Erwin Mesias · Answer · Learn more efficiently, for free: Introduction to Python · 7.1M learners ·
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching scalar type available, e.g. int16). Therefore, the use of array scalars ensures identical behaviour between arrays and scalars, irrespective of whether the value is inside an array or not.
🌐
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.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 47294
BUG: uint16 inserted as int16 when assigning row with dict · Issue #47294 · pandas-dev/pandas
June 9, 2022 - This also happens when assigning into an existing object-typed column (the conversion sequence seems to be -> int16 -> int in that case). It's expected the dtype is preserved - uint16 if possible, or an int which is large enough to represent the value. python : 3.8.10.final.0 python-bits : ...
Author   bluss
🌐
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.
🌐
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!
🌐
Reddit
reddit.com › r/learnpython › int16 convertion differences between python and matlab
r/learnpython on Reddit: int16 convertion differences between python and matlab
November 28, 2018 -

Hello all,

I have been using matlab for many years, and I'm slowly converting to python. I have a question regarding the handling of integer values, specifically int16. It does not seem that the two programs behave the same way. An example to illustrate my point:

In matlab if I type : cast([-1:0.2:1] * 2^15,'int16') the answer I get is:

-32768 -26214 -19661 -13107 -6554 0 6554 13107 19661 26214 32767

equivalently in python if I type: (np.arange(-1,1.2,0.2).astype('float64') * 2**15).astype('int16') the answer I get is:

-32768, -26214, -19660, -13107, -6553, 0, 6553, 13107, 19660, 26214, 32767

Note how the third, fourth, sixth and eight value are not the same.

This concerns me because if I compare the matlab versus python float values, their differences are in the order of 10**-16 which is much lower than the half bit difference in int16 which is in the order of 10**-5 ...in other words I do not believe this is a rounding error because of the numerical conversion, but something else entirely.

What is the cause of this discrepancy?

Any help is greatly appreciated.

A.