You could discard the bottom 16 bits:

n=(array_int32>>16).astype(np.int16)                          

which will give you this:

array([ 485, 1054, 2531], dtype=int16
Answer from Mark Setchell on Stack Overflow
๐ŸŒ
ProgramCreek
programcreek.com โ€บ python โ€บ example โ€บ 5680 โ€บ numpy.int16
Python Examples of numpy.int16
Args: pointer : int or ctypes pointer address of a buffer count : int Number of items to read. dtype : data-type, optional Data-type of the returned array; default: float. Examples: >>> s = numpy.ones(3, dtype=numpy.int32) >>> ptr = s.ctypes.data >>> frompointer(ptr, count=6, dtype=numpy.int16) [1, 0, 1, 0, 1, 0] ''' dtype = numpy.dtype(dtype) count *= dtype.itemsize buf = (ctypes.c_char * count).from_address(pointer) a = numpy.ndarray(count, dtype=numpy.int8, buffer=buf) return a.view(dtype) Example #8 ยท
Discussions

Python Unsigned 16-bit Integer Array Elements - Stack Overflow
Now I'm curious how numpy does ... to array. 2022-05-12T05:35:05.333Z+00:00 ... Yes, logging this potential scenario would def be useful. Regarding numpy.uint16, I'm not 100% certain that numpty forces the 2 byte size either... it's just that the spec for using "H" (unsigned short) lists 2 bytes at the 'minimum' size in bytes: docs.python.org/3/lib... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - convert numpy int16 audio array to float32 - Stack Overflow
@jaket what if I have numpy array of audio file and I want to detect words from it?? 2023-01-18T05:52:44.69Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Community Asks Sprint Announcement โ€“ January 2026: Custom site-specific badges! Stack Overflow chat opening up to all users in January; Stack Exchange chat... ... 0 Converting Python ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - What is an efficient way to scale a int16 numpy array to a int8 numpy array - Stack Overflow
Im trying to find an efficient way to scale 2 byte (-32K -> +32K) numpy int arrays to 8 bit (0 -> 255) using a specific scaling function. The very inefficient method that works is (where minVal and More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - convert binary array to int16 - Stack Overflow
the array is a waveform. I can easily convert a binary array to int8, when each byte is 1 sample. when working with 12bit, I can set the instrument to send 2bytes (word mode) for each sample. I hav... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 42277127 โ€บ bytes-into-numpy-int16-array
python 3.x - Bytes into Numpy Int16 array - Stack Overflow
you can use np.frombuffer. do you want to combine two bytes into int16 or one int16 for each byte? first case use .view. second case use .astype- I think you can even specify the dtype in frombuffer but not sure. That would work in the first case. ... Alternatively, you could use the sounddevice module, which directly supports NumPy arrays and does the conversion for you if you specify dtype='int16'.
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ understanding-numpy-int16-and-numpy-uint16-types-6-examples
Understanding numpy.int16 and numpy.uint16 types (6 examples) - Sling Academy
Here lies their primary difference โ€“ the range of values they can represent. numpy.int16 can represent values from -32768 to 32767, whereas numpy.uint16 can represent values from 0 to 65535. import numpy as np # Creating arrays with numpy.int16 and numpy.uint16 types data_int16 = np.array([32767, ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ array.html
array โ€” Efficient arrays of numeric values
This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ...
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ PythonDataScienceHandbook โ€บ 02.01-understanding-data-types.html
Understanding Data Types in Python | Python Data Science Handbook
At the implementation level, the array essentially contains a single pointer to one contiguous block of data. The Python list, on the other hand, contains a pointer to a block of pointers, each of which in turn points to a full Python object like the Python integer we saw earlier.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 42558112 โ€บ convert-binary-array-to-int16
python - convert binary array to int16 - Stack Overflow
data = numpy.array(unpack('%sh' % len(dataword) / struct.calcsize('h'), dataword)) #----------------------------^ notice the 'h', signed short, 16 bits
๐ŸŒ
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.

๐ŸŒ
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. The default array index data type may be int32 on 32-bit platforms, but the default should be int64 otherwise.