Python Doubt regarding a basic Python operator

AI Thread Summary
The discussion focuses on the bitwise NOT operator (~) in Python, explaining that it flips each bit in the binary representation of a number, turning ones into zeros and vice versa. This operation can lead to a negative result due to the way computers store negative numbers using two's complement, which interprets a binary number with a leading one as negative. The conversation highlights the importance of understanding two's complement for grasping why a negative sign appears after using the bitwise NOT operator. Additionally, participants inquire about practical applications of one's and two's complement, as well as how to manually apply the bitwise NOT operator without a programming console. Resources for further learning, including tutorials on bit manipulation and two's complement, are shared to assist beginners in deepening their understanding.
jishnu
Messages
73
Reaction score
15
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 textbook (unofficial) with this post and I am confused how that negative sign comes after the operation. Please provide me relevant links of sites where I can learn these things more clearly.
Thanks in advance
[emoji846]
Screenshot_2019-01-21-23-26-05-025_com.microsoft.office.word.jpeg
 

Attachments

  • Screenshot_2019-01-21-23-26-05-025_com.microsoft.office.word.jpeg
    Screenshot_2019-01-21-23-26-05-025_com.microsoft.office.word.jpeg
    57.6 KB · Views: 973
Technology news on Phys.org
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.
 
  • Like
Likes 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.
 
Ibix said:
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!
 
jishnu said:
And most importantly how did that 61 came in the answer after the operation!
Read about two's complement!
 
  • Like
Likes jishnu
Sarah Hrmbree said:
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!
 
Thanks allot guys!
[emoji3531]
 

Similar threads

Replies
10
Views
3K
Replies
42
Views
5K
Replies
4
Views
2K
Replies
2
Views
957
Replies
7
Views
2K
Replies
3
Views
1K
Replies
15
Views
2K
Replies
14
Views
2K
Replies
3
Views
1K
Back
Top