# Python Doubt regarding a basic Python operator

#### jishnu

Hi everyone,
I am beginner in python programming. So many doubts are being generted in the learing process
Can anyone please explain me how the bitwise NOT (~) operator actually works on values. I have attached a screen short of my text book (unofficial) with this post and I am confused how that negative sign comes after the operation. Please provide me relevent links of sites where I can learn these things more clearly.
Thanks in advance
[emoji846]

#### Attachments

• 73.7 KB Views: 177
Related Programming and Computer Science News on Phys.org

#### Ibix

Science Advisor
The bitwise not operator simply flips each one in the binary representation of a number to a zero, and vice versa. You can see this in the example.

Where the negative sign comes in is related to how computers store negative numbers. Look up two's complement:
https://en.m.wikipedia.org/wiki/Two's_complement
In short, a computer will generally interpret a binary number whose most significant bit is a 1 as a negative number unless it's told otherwise (edit: that may be a bit of an overstatement, but as far as I'm aware signed numbers are (almost?) always stored using two's complement). Since, in the example, a is positive its first digit is zero; flipping that makes the result a negative number.

#### Sarah Hrmbree

It depends a lot on what you are trying to do, what data you are working with.

for i in range(-5,6):
print(i, "->", ~i)

will show you the basic basics. Beyond that there are many tutorials about working with bit arrays, bit fields, etc in Python out there. Be specific and you can get better, more applicable answers.

#### jishnu

The bitwise not operator simply flips each one in the binary representation of a number to a zero, and vice versa. You can see this in the example.

Where the negative sign comes in is related to how computers store negative numbers. Look up two's complement:
https://en.m.wikipedia.org/wiki/Two's_complement
In short, a computer will generally interpret a binary number whose most significant bit is a 1 as a negative number unless it's told otherwise (edit: that may be a bit of an overstatement, but as far as I'm aware signed numbers are (almost?) always stored using two's complement). Since, in the example, a is positive its first digit is zero; flipping that makes the result a negative number.
That is ohk.
But, is there some other purpose or application in taking one's complement and two's complement of any number?
And most importantly how did that 61 came in the answer after the operation!

#### Ibix

Science Advisor
And most importantly how did that 61 came in the answer after the operation!
Read about two's complement!

#### jishnu

It depends a lot on what you are trying to do, what data you are working with.

for i in range(-5,6):
print(i, "->", ~i)

will show you the basic basics. Beyond that there are many tutorials about working with bit arrays, bit fields, etc in Python out there. Be specific and you can get better, more applicable answers.
My doubt is regarding how we actually apply that bitwise NOT operator in practice to reach the answer without using the python console for programing!

#### jishnu

Thanks allot guys!
[emoji3531]

### Want to reply to this thread?

"Doubt regarding a basic Python operator"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving