Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use high and low bytes?

I am trying to represent 32768 using 2 bytes. For the high byte, do I use the same values as the low byte and it will interpret them differently or do I put the actual values? So would I put something like 32678 0 or 256 0? Or neither of those? Any help is appreciated.

like image 337
Sterling Avatar asked May 22 '11 20:05

Sterling


People also ask

What is high and low byte?

The bits of a word are numbered from 0 through 15; bit 0 is the least significant bit. The byte containing bit 0 of the word is called the low byte; the byte containing bit 15 is called the high byte.

What does low byte mean?

The low byte is the byte that holds the least significant part of an integer. If you think in terms of writing a bit pattern on paper, the low byte is the rightmost eight bits. A short holds a 16-bit pattern such as: 01001010 00001111. The low order byte is 00001111 .

How much data is in a byte?

The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures.

How many bits is a double word?

A byte is eight bits, a word is 2 bytes (16 bits), a doubleword is 4 bytes (32 bits), and a quadword is 8 bytes (64 bits).


2 Answers

In hexadecimal, your number is 0x8000 which is 0x80 and 0x00. To get the low byte from the input, use low=input & 0xff and to get the high byte, use high=(input>>8) & 0xff.

Get the input back from the low and high byes like so: input=low | (high<<8).

Make sure the integer types you use are big enough to store these numbers. On 16-bit systems, unsigned int/short or signed/unsigned long should be be large enough.

like image 54
John McFarlane Avatar answered Oct 05 '22 22:10

John McFarlane


Bytes can only contain values from 0 to 255, inclusive. 32768 is 0x8000, so the high byte is 128 and the low byte is 0.

like image 29
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 23:10

Ignacio Vazquez-Abrams