Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why is ~0 a sequence of 1 bits and not just a single bit? Where do the extra 1 bits come from? Does this mean ~1 a sequence of 0 bits or just a single 0 bit?

My understanding was that the ~ operator is applied on the specific bits mentioned.

share|improve this question

closed as unclear what you're asking by gnat, Kilian Foth, BЈовић, MichaelT, GlenH7 Feb 27 '14 at 14:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 19 down vote accepted

because 0 in the chosen representation (2's complement) is a sequence of 0s, applying the bitwise NOT on it will naturally result in a sequence of 1s

1 is a sequence of 0s followed by a single 1

Remember that a computer will use all bits available to represent a number in a variable. In other words if you have a 64 bit variable then all 64 bits will be in use even if 90% of them are 0.

share|improve this answer
Thanks. I wasn't thinking about 0 being the same as 00000...0. So does that mean ~1 (say 1 is 0000001) becomes 0 or 1111110? – clicky Feb 26 '14 at 11:33
@clicky 1111110. Those other bits don't disappear when you store 1, they are merely set to zero and hence affected by ~. – delnan Feb 26 '14 at 11:42

Because that is what ~ does.

It flips the bits of the operand. If your language of choice treats that literal 0 as a byte value, it is essentially:


When the bites are flipped using the ~ (bitwise NOT), the result is:


In the same way that ~10 would turn:



share|improve this answer
Ah. 0 is the same as 000000...0, obvious but didn't think of that. Thanks. – clicky Feb 26 '14 at 11:29
@clicky - yeah, you need to think about how the computer represents the value internally. – Oded Feb 26 '14 at 11:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.