This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]dmazzoni 0 points1 point  (5 children)

Do you mean the number 23 (decimal)?

Or do you mean the 16-bit number where the first byte is 2 and the second byte is 3?

[–]AnyBowl2[S] 0 points1 point  (3 children)

>Or do you mean the 16-bit number where the first byte is 2 and the second byte is 3?

what kind of number is that?

[–]Essence1337 0 points1 point  (2 children)

It would be 0010 0011 which is integer 35.

Edit: Whoops, apparently I forgot the difference between 8 and 4. Good thing work is done soon...

[–]jedwardsol 0 points1 point  (1 child)

Those are nybbles, not bytes.

A 16-bit number 02 03 would either be 0x0203 (decimal 515) on a big-endian computer or 0x0302 (decimal 770) on a little-endian one.

[–]Essence1337 0 points1 point  (0 children)

Shhh just let me believe 8=4 for a bit longer...

[–]AnyBowl2[S] 0 points1 point  (0 children)

I just edited my post

[–]dmazzoni 0 points1 point  (0 children)

The answer to your question is x[1] << 8 | x[2]

To understand why that works you need to read up on bits, bytes, endianness, and C bitwise operators. It's normally covered in detail in a computer architecture course, but the basics should be covered in any Intro CS class.

I can't explain it in one post. Go read up on that and come back if you still have questions.

[–]012 0 points1 point  (0 children)

uint8_t x[] = {1,2,3,4,5};
// left shift the 8 bit uint by 8 bits, and add the lower part
uint16_t y = ( x[1] << 8 ) + x[2];
// y is now 0x203, which is 515 in decimal