Doubt regarding a basic Python operator

In summary, the bitwise not operator flips each one in the binary representation of a number to a zero, and vice versa. You can see this in the example.
  • #1
jishnu
73
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: 913
Technology news on Phys.org
  • #2
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
  • #3
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.
 
  • #4
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!
 
  • #5
jishnu said:
And most importantly how did that 61 came in the answer after the operation!
Read about two's complement!
 
  • Like
Likes jishnu
  • #6
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!
 
  • #8
Thanks allot guys!
[emoji3531]
 

1. What is a basic Python operator?

A basic Python operator is a symbol or keyword used to perform mathematical or logical operations on data. Examples include addition (+), subtraction (-), multiplication (*), and division (/).

2. How do I use a basic Python operator?

To use a basic Python operator, you need to first identify the data or variables you want to perform the operation on, and then use the appropriate operator symbol between them. For example, to add two numbers together, you would use the + symbol between the numbers.

3. Can I use basic Python operators on different data types?

Yes, you can use basic Python operators on different data types. However, the results may differ depending on the data types being used. For example, adding two strings together will concatenate them, while adding two integers will perform a mathematical addition.

4. Are there any rules for using basic Python operators?

Yes, there are some rules for using basic Python operators. For example, the order of operations follows the standard mathematical rules of PEMDAS (parentheses, exponents, multiplication/division, addition/subtraction). Additionally, some operators, such as the division operator (/), may behave differently depending on the data types being used.

5. Can I create my own custom operators in Python?

No, it is not possible to create custom operators in Python. However, you can create functions or methods to perform specific operations on data, and then call them in your code as needed.

Similar threads

Replies
6
Views
544
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
630
  • Programming and Computer Science
Replies
7
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
677
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top