Python Doubt regarding a basic Python operator

Click For Summary
SUMMARY

The discussion focuses on the functionality of the bitwise NOT operator (~) in Python programming. It explains that this operator flips each bit in the binary representation of a number, converting ones to zeros and vice versa. The negative sign observed in the result is due to the two's complement method used by computers to represent negative numbers. The user is encouraged to explore practical applications of the bitwise NOT operator and to refer to additional resources for deeper understanding.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with binary number representation
  • Knowledge of two's complement for negative number representation
  • Experience with Python's bitwise operators
NEXT STEPS
  • Study the implementation of bitwise operators in Python
  • Learn about two's complement and its applications in programming
  • Explore practical use cases of bitwise operations in real-world scenarios
  • Review tutorials on manipulating bit arrays and bit fields in Python
USEFUL FOR

This discussion is beneficial for beginner Python programmers, computer science students, and anyone interested in understanding bitwise operations and their applications in programming.

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: 988
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
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 43 ·
2
Replies
43
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K