Reputation: 2541
I am facing a following problem: When trying to do cast to an unsigned char I get unexpected values. The code that I am using:
unsigned char MyVal1 = ((0xF1E3 && 0xff00) >> 8);
unsigned char MyVal2 = (unsigned char)((0xF1E3 && 0xff00) >> 8);
unsigned char MyVal3 = (unsigned char)((0xF1E3 && 0xff));
I am storing all three variables in an array.
The output I am getting (looking at the values in array; array is unsigned char array):
0x00
0x00
0x01
while I was expecting:
0xF1
0xF1
0xE3
Could someone be kind to help me out in what am I doing wrong?
Upvotes: 3
Views: 1458
Reputation: 157484
&&
is the boolean and operator; it gives 1
if both its operands are non-zero and 0
otherwise. You want the bitwise and operator, &
, which gives 1
or 0
in each bit of its operands.
Upvotes: 10
Reputation: 13589
Operators &&
and &
do not work the same on integers. Your operands are first converted to bool (zero/nonzero) and then and
ed together.
Upvotes: 12