Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how convert two bytes into one 16-bit number?

I understand that 1 byte will hold a number from 0-255. And that a 16-bit number is between 0-65535.

If I'm trying to represent a 16-bit number using two separate 8-bit registers...how do I do that? How does the math work?

Thanks!

like image 793
Jay Kim Avatar asked Jun 01 '12 16:06

Jay Kim


People also ask

What is a 2 byte integer?

2-byte signed Integer [the ~] noun – An automation integer data type that can be either positive or negative. The most significant bit is the sign bit, which is 1 for negative values and 0 for positive values. The storage size of the integer is 2 bytes. A 2-byte signed integer can have a range from -32,768 to 32,767.

How do you represent 16 bits?

16-bit signed numbersThe smallest signed 16-bit number is -32768 and the largest is 32767. For example, 1101,0000,0000,01002 or 0xD004 is -32768+16384+4096+4 or -12284.

How do you combine two bytes in Python?

To join a list of Bytes, call the Byte. join(list) method. If you try to join a list of Bytes on a string delimiter, Python will throw a TypeError , so make sure to call it on a Byte object b' '. join(...)

How many characters can a 16 bit number represent?

The first code point positions in Unicode use 16 bits to represent the most commonly used characters in a number of languages. This Basic Multilingual Plane allows for 65,536 characters.


1 Answers

The math works out as follows:

sixteenBitNumber = 256*upperByte + lowerByte;

with shifts and bitwise operations:

sixteenBitNumber = (upperByte<<8) | lowerByte;

In most CPUs, even some archaic 8-bit ones, this interpretation is done in hardware: you load bytes into parts of a 16-bit register or into separate 8-bit registers that can work as a 16-bit pair, and the hardware works with the data as if it were a single 16-bit number.

like image 59
Sergey Kalinichenko Avatar answered Sep 27 '22 18:09

Sergey Kalinichenko