Here is a solution with structs since I do not know how python represents negative numbers binarily.

import struct
struct.unpack('H', struct.pack('h', number))

It packs it as a short (2 bytes) and unpacks it as an unsigned short.

Answer from User on Stack Overflow
🌐
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.

Discussions

Receiving 16-bit integers in Python - Stack Overflow
I'm reading 16-bit integers from a piece of hardware over the serial port. Using Python, how can I get the LSB and MSB right, and make Python understand that it is a 16 bit signed integer I'm fidd... More on stackoverflow.com
🌐 stackoverflow.com
c# - How to convert from string to 16-bit unsigned integer in python? - Stack Overflow
I'm currently working on some encoding and decoding of the string in python. I was supposed to convert some code from C# to python, however I encountered some problem as below: So now I have a str... More on stackoverflow.com
🌐 stackoverflow.com
October 4, 2019
Python Unsigned 16-bit Integer Array Elements - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Converting integer to 16-bit hexadecimal in Big-endain form

Check out hex() and, more importantly struct:

http://docs.python.org/2/library/struct.html

This lets you convert a decimal into binary data. I've used it to read binary files, so it might be what you are looking for. I'm weak on theory so I tend to just bang on stuff until I get it right, but I would start with:

>>> import struct
>>> struct.pack('>h', -200)
b'\xff8'

And see what the machine makes of it, and adjust accordingly.

More on reddit.com
🌐 r/learnpython
12
4
March 6, 2014
🌐
Python
mail.python.org › pipermail › python-list › 2003-April › 211404.html
hex -> 16bit signed int (newbie)
April 15, 2003 - >>> hexstr = 'AB0E' >>> s = '' ... machine-dependent; on my machine it's 32 bits, for example. If you want 16-bit unsigned on all machines, you want '>h' or some such....
🌐
GitHub
gist.github.com › 711360947b40e8cc404e
Convert int to 16 bits in python · GitHub
Convert int to 16 bits in python · Raw · convert_16bits.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › converting integer to 16-bit hexadecimal in big-endain form
r/learnpython on Reddit: Converting integer to 16-bit hexadecimal in Big-endain form
March 6, 2014 -

I'm trying to take a number (such as -200) and send it to a robot. The number must be converted to big-endian and sent in two bytes. WolframAlpha does this (the result I need to send is 38ff) but I haven't found a way to do this in python.

🌐
Quora
quora.com › How-can-I-compress-a-16-bit-integer-to-an-8-bit-size-integer-in-Python
How to compress a 16-bit integer to an 8-bit size integer in Python - Quora
As Tony Flury points out, though, ... to do in Python - you would be better off trying it in C, which is well suited to working with bits at a low level. Is it worth doing? For one single number, it isn’t - you would save 8 bits, and probably use up several hundred bytes (at least) on the compression code, even using C. If you had a large number of small values it might be worth doing. ... Compressing a 16-bit signed or unsigned integer into 8 bits ...
🌐
Reddit
reddit.com › r/learnprogramming › combining two 16 bit signed integer numbers to produce the 32 bit integer equivalent using python 3.
r/learnprogramming on Reddit: Combining two 16 bit signed integer numbers to produce the 32 bit integer equivalent using Python 3.
October 18, 2019 -

I have a 32 bit number A = A3A2A1A0 that has been given to me in the form of A = B1B0 where B1 = A3A2 and B0 = A1A0. B1 and B0 are each 16 bit numbers. I now want to use B1 and B0 to get A in an unsigned integer format.

I tried using the 2's complement representation of B1 and B0 but am still not clear as to how I would "add" those two binary numbers.

I also tried using the bin(integer) function in Python to get the binary representation of both B1 and B0 in a list format and then concatenated the two lists to give me A. However, that didn't seem to work if either of B1 or B0 were negative.

I'm not quite understanding how B1 is getting affected when B0 goes into the overflow, i.e crosses 32768 to give a negative number.

If anyone could give me few examples taking B1 and B0 to be positive and negative both, with the final answer A as well that would be great. Any hints and suggestions would be appreciated. Thank you.

🌐
Readthedocs
iec104-python.readthedocs.io › latest › python › number › uint16.html
Usigned Integer (16-bit) — iec104-python 2.2 documentation
c104 python module · Number · Usigned Integer (16-bit) View page source · class c104.UInt16 · __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 ·
🌐
Bomberbot
bomberbot.com › python › converting-signed-to-unsigned-integers-in-python-a-deep-dive
Converting Signed to Unsigned Integers in Python: A Deep Dive - Bomberbot
In this example, we use the struct module to unpack binary data and the bitwise AND operation to ensure length is treated as an unsigned 16-bit integer.
🌐
Quora
quora.com › How-do-I-see-the-type-of-a-variable-whether-it-is-unsigned-32-bit-signed-16-bit-etc-using-Python
How to see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc. using Python - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
signed int - Raspberry Pi Forums
RPi Information Screen: plugin ... 22, 2017 1:09 pm · read_registers(registeraddress, numberOfRegisters, functioncode=3)[source] Read integers from 16-bit registers in the slave....
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 359675 › signed-and-unsigned-int-in-python
Signed and Unsigned int in python [SOLVED] | DaniWeb
If the device/MIB expects unsigned 8- or 16-bit values, do not concatenate text; work with bytes and bitwise operations. Take the numeric values you read, mask them to the desired width, and pack them to bytes in the correct byte order.