Sterling
Sterling

Reputation: 201

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.

Upvotes: 20

Views: 89345

Answers (6)

cyberponk
cyberponk

Reputation: 1766

Pointers can do this easily, are MUCH FASTER than shifts and requires no processor math.

Check this answer

BUT: If I understood your problem, you need up to 32768 stored in 2 bytes, so you need 2 unsigned int's, or 1 unsigned long. Just change int for long and char for int, and you're good to go.

Upvotes: 2

Ernesto A. Garrote
Ernesto A. Garrote

Reputation: 41

Try this function. Pass your Hi_Byte and Lo_Byte to the function, it returns the value as Word.

WORD MAKE_WORD( const BYTE Byte_hi, const BYTE Byte_lo)
{
     return   (( Byte_hi << 8  ) | Byte_lo & 0x00FF );
}

Upvotes: 4

John McFarlane
John McFarlane

Reputation: 6087

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.

Upvotes: 31

azyoot
azyoot

Reputation: 1172

32768 in hex is 0080 on a little-endian platform. The "high" (second in our case) byte contains 128, and the "low" one 0.

Upvotes: 0

Alnitak
Alnitak

Reputation: 339806

32768 is 0x8000, so you would put 0x80 (128) in your high byte and 0 in your low byte.

That's assuming unsigned values, of course. 32768 isn't actually a legal value for a signed 16-bit value.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798576

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.

Upvotes: 5

Related Questions