Wingx2
March 17, 2020, 2:09am
1
Hello!
I want to print 8 bits of a byte on serial monitor, now is work fine !
mbyte = 5 , print result 00000101 is correct.
void setup() {
Serial.begin(9600);
int num = 5;
byte mbyte = num;
for (int i = 0; i < 8; i++) {
bool b = mbyte&0x80;
Serial.print(b);
mbyte = mbyte << 1;
}
Serial.println();
}
void loop(){}
but one thing is still not clear !
if i change (mbyte = 5)
bool b = mbyte&0x80;
to
bool b = mbyte;
print result = 11111111
0x80 = 10000000
So i don't understand, why increase &0x80, will get print correct result 00000101 ?
use "bitwise AND" think compare numbers(get a 1 if both bits are 1) for 11111111
i can't get result 00000101.
Thanks!
Wing
hint: tryint b = mbyte;and see what is printed
Wingx2:
int num = 5;
byte mbyte = num;[/code]
In programming, I like to maintain variable's data type and its value consistent even by casting if necessary.
1. Your quoted codes are re-written as:
int num = 5;
int mbyte = num;
2. Adding 0x1234 and 0x56
int x1 = 0x1234;
byte x2 = 0x56;
int sum = x1 + (int)x2;
Serial.print(sum, HEX);
Robin2
March 17, 2020, 8:38am
4
I commented on this in another Thread as follows
0x80 is 0b10000000
when you use bitwise AND you get a 1 if both bits are 1, otherwise you get a 0
Using & 0x80 will ensure that all bits except the leftmost bit will be 0. The value of the leftmost bit will depend on the value at that bit position in the other number
...R
westfw
March 17, 2020, 9:20am
5
bool b = mbyte&0x80;
Is doing more than you expect. It’s equivalent to:
If ((mybyte&0x80) != 0) b=1; else b=0
Without the &0x80 to isolate a single bit, 5 will stay non-zero for a long time after shifting...
Wingx2
March 17, 2020, 10:57am
6
aarg:
hint: try
int b = mbyte;
and see what is printed
Hello:
int b = mbyte;
b=5
with forloop "mbyte = mbyte << 1;"
b=5,10,20,40,80,160.......
GolamMostafa:
In programming, I like to maintain variable's data type and its value consistent even by casting if necessary.
1. Your quoted codes are re-written as:
int num = 5;
int mbyte = num;
**2.** Adding 0x1234 and 0x56
int x1 = 0x1234;
byte x2 = 0x56;
int sum = x1 + (int)x2;
Serial.print(sum, HEX);
Thank you for your experience
Robin2:
I commented on this in another Thread as follows
...R
Hello!
Yes, I saw it, Thanks
but Thread is old, i want make new Thread to make clear concept "&0x80".
Wingx2
March 17, 2020, 11:03am
7
westfw:
bool b = mbyte&0x80;
Is doing more than you expect. It’s equivalent to:
If ((mybyte&0x80) != 0) b=1; else b=0
Without the &0x80 to isolate a single bit, 5 will stay non-zero for a long time after shifting...
Thank you for your reply, that makes me clearer
I found the information based on your reply
Why if(byte & 0x80) means "If the highest bit in the byte isn't 0?" <<
Wingx2
March 17, 2020, 11:29am
8
westfw:
bool b = mbyte&0x80;
Is doing more than you expect. It’s equivalent to:
If ((mybyte&0x80) != 0) b=1; else b=0
Without the &0x80 to isolate a single bit, 5 will stay non-zero for a long time after shifting...
Hello! One more question
"&0x80"
That "&" mean Bitwise Operators And ??
Thanks!