Question of Understanding - Bitwise Shift

Hi there,

i am fairly new to c++ and today i am learning bitwise shifting.
My case - I want to split an uint16 into its 2 bytes.

A solution i found was:

unsigned char high = (val >> 8) & 0xFF;
unsigned char low = val & 0xFF;

Why would i need bitwise and for high? If i use it without, i get the same results...

unsigned char high = val >> 8; // ohne bitwise and
unsigned char low = val & 0xFF;

What am i missing out? Nothing?!

Thank you in advance and have a great day!

Best regards

On assignment to an unsigned byte-size variable any & 0xFF can be omitted. Except for bool variables.

Given:

unsigned int x = 0x1234;

byte upperByte = highByte(x);   //upperByte = 0x12
byte lowerByte = lowByte(x);     //lowerByte = 0x34

Interesting. I assume that from a time complexity standpoint, the call to high- and lowByte() would be the slower solution?! Is this correct?

Hi
Assuming val contains 0x0AB4
val = 0B 0000 1010 1011 0100
high = (val >> 8) = nnnn nnnn 0000 1010 0Xnn0A
&-----------------------------------------1111 1111 0XFF
Result ..........................................0000 1010 high = 0X0A

low= (val 8) = ...... 0000 1010 1011 0100 0X0AB4
&---------------------------------------1111 1111 0XFF
Result .......................................1011 0100 low = 0XB4

Since type char is 1 byte the upper byte is automatically lost. But I think the official ANSI/ISO C++ standard says a char is 1 byte or more so it would depend on the particular C++ implementation.

There's a typo on that bottom line.

Anyway, this demonstrates that the & does nothing in this case.

Tks
I've already corrected

Could be! No idea!

Ah, thank you. Anyway, it still demonstrates that the bitwise AND does nothing.

Nothing noticeable is not nothing. Assign the result to an int variable and you see what the AND does.

Yes, of course, but this whole discussion has been about assigning the result to byte variables - see the first post.

val is 16-bit; so, it should be ANDed with 16-bit value. That is --

unsigned char low = val & 0x00FF; //val = 0x00XX

Because, the receiver identifier has been declared as 8-bit (char-type), the Compiler will automatically assign lower 8-bit of val.

We are talking about uint16, nothing else. The only reason to "bitwise or" the high would be that i have to handle more than 16 bits of data. But if there is only 16 bit, my thinking is that i dont need to "cast" the high part to 8 bits by using an "bitwise or" with 0xff. As i stated, the results are the same. I was thinking about that there might exist edge cases, even with uint16, due to my lack of c++ skills as of know.

Not sure about those zeros there. 0xff and 0x00ff is exactly the same in binary... Whats the point?

In fact, the compiler will very rarely actually emit an AND instruction. Or the shift, for that matter. Compiler Explorer

The OP code extracts the 0-7 and 8-15 bits into individual bytes in a way that is portable across most CPUs and compilers, regardless of the exact sizes of the 'int' and "char' data types.