Hi mu question is very simple .
when i try use bitewise or the result wrong. for example
void setup() {
int result;
delay(50);
result=0x0000 | 0x8000;
delay (500);
Serial.println (result,HEX);
}
result must be 8000 but after running programm result FFFF8000. I mean it add 16 bit more. how can it be. Thanks
What happens if "result" is an unsigned int?
(Please rest assured, you haven't added sixteen bits)
math or result fault
Nope, Pilot Error.
Since 'result' is an 'int', this:
result=0x0000 | 0x8000;
Produces a negative number on an 8-bit processor (as @AWOL opined). 0x8000 = -32768 (decimal).
When you do:
Serial.println (result,HEX);
The print function casts it as a 'long' which sign extends it to 32 bits: 0xFFFF8000.