Skip to main content
Tweeted twitter.com/#!/StackProgrammer/status/173238121662656512
added 68 characters in body
Source Link
pdu
  • 579
  • 1
  • 6
  • 21

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 10. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).

Edit 1 Thanks to mcfinnigan's comment I learned that there is a "most significant bit" that defines if a number is positive or negative (correct me if I'm wrong). This would explain why positive results are ending up negative and vice versa. However, IMO the example above would result in -10 then.

Edit 2 Thanks to Don 01001100 and S.Lott, I finally got it.

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 10. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).

Edit 1 Thanks to mcfinnigan's comment I learned that there is a "most significant bit" that defines if a number is positive or negative (correct me if I'm wrong). This would explain why positive results are ending up negative and vice versa. However, IMO the example above would result in -10 then.

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 10. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).

Edit 1 Thanks to mcfinnigan's comment I learned that there is a "most significant bit" that defines if a number is positive or negative (correct me if I'm wrong). This would explain why positive results are ending up negative and vice versa. However, IMO the example above would result in -10 then.

Edit 2 Thanks to Don 01001100 and S.Lott, I finally got it.

edit 1
Source Link
pdu
  • 579
  • 1
  • 6
  • 21

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 910. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).

Edit 1 Thanks to mcfinnigan's comment I learned that there is a "most significant bit" that defines if a number is positive or negative (correct me if I'm wrong). This would explain why positive results are ending up negative and vice versa. However, IMO the example above would result in -10 then.

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 9. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 10. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).

Edit 1 Thanks to mcfinnigan's comment I learned that there is a "most significant bit" that defines if a number is positive or negative (correct me if I'm wrong). This would explain why positive results are ending up negative and vice versa. However, IMO the example above would result in -10 then.

Source Link
pdu
  • 579
  • 1
  • 6
  • 21

How does bit flipping / complementing work?

I am currently learning about bitwise operation, so bear with me. I understand AND, OR, and shifting. What I don't understand is bit flipping.

So, 5 is 0101. When someone says to me "flip those", it would result in 1010 which is 9. So why does ~5result in -6?

I think I got this very wrong. Can someone enlight me?

(I am putting this here because I think it is a general programming question and not a specific one, which would belong to stack overflow).