Doubt regarding a basic Python operator

Click For Summary

Discussion Overview

The discussion revolves around the functionality and application of the bitwise NOT (~) operator in Python programming. Participants explore how the operator works, particularly in relation to binary representation and negative number storage, as well as practical applications of bitwise operations.

Discussion Character

  • Exploratory, Technical explanation, Conceptual clarification, Homework-related

Main Points Raised

  • One participant seeks clarification on how the bitwise NOT operator works and why it results in a negative value, expressing confusion over a textbook example.
  • Another participant explains that the operator flips bits in a binary representation and relates the negative sign to two's complement representation of negative numbers, suggesting further reading on the topic.
  • Some participants mention that the application of the operator can depend on the specific data being used and suggest that practical examples can help clarify its use.
  • There is a repeated inquiry about the origin of a specific numerical result (61) after applying the operator, indicating a need for further explanation.
  • One participant provides a link to a resource discussing real-world use cases of bitwise operators, suggesting it may help the original poster.

Areas of Agreement / Disagreement

Participants generally agree on the basic functionality of the bitwise NOT operator but express varying levels of understanding regarding its implications and applications. The discussion remains unresolved regarding the specific numerical example and the practical application of the operator without using a programming console.

Contextual Notes

There are limitations in the explanations provided, particularly regarding the assumptions made about the reader's prior knowledge of binary representation and two's complement. Some participants express uncertainty about the broader applications of bitwise operations.

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: 996
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   Reactions: 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   Reactions: 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
4K
  • · Replies 43 ·
2
Replies
43
Views
7K
  • · 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