简体   繁体   中英

Bitwise & and I

Why is 1 & 4 = 0

whereas 1 | 4 evaluates to 5

Well.. because.

For & , the AND operator:

0001       = 1
0100       = 4
---- (AND)
0000       = 0

for | , the OR operator:

0001       = 1
0100       = 4
---- (OR)
0101       = 5

Bitwise & => If both bits are higher, then the output is higher else output is zero.

0 0 1
1 0 0
-----
0 0 0  => 0   // 1 & 1 = 1 , 1 & 0 = 0

Now try yourself Bitwise | . Any of the bit is higher, output is higher.

10b001 ,和40b100 ,那么,自然, 1&40b0001|40b101 ,这是5

Look at it in binary form.

1d(ecimal) = 001b(inary)

4d(ecimal) = 100b(inary)

thus

001b
100b & (both bits have to be 1 to yield 1)
--
000b = 0d

and

001b
100b | (only one on either side (or both) has to be 1 to yield 1)
--
101b = 5d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM