简体   繁体   中英

Bitwise operator with negative numbers

Why does..

-23&30 = 8
5&-3 = 5 
15&-1 = 15

I understand & with positive numbers but for some reason when a negative number is thrown, I don't understand how the answer is derived.

You should read about 2's complement method of representing negative numbers in binary.

For example:

 5 == 00000000 00000000 00000000 00000101
 &
-3 == 11111111 11111111 11111111 11111101
 =    -----------------------------------
 5 == 00000000 00000000 00000000 00000101

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