user10382706
user10382706

Reputation:

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.

Upvotes: 0

Views: 171

Answers (1)

Eran
Eran

Reputation: 393771

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

Upvotes: 4

Related Questions