What is the bit-wise NOT operator in Python? [duplicate] - python

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed last month.
Is there a function that takes a number with binary numeral a, and does the NOT?
(For example, the function's value at 18 [binary 10010] would be 13
[binary 01101].) I thought this was what the tilde operator (~) did, but it only adds a minus sign to 18, which is two's complement of that, instead of getting 13.

As mentioned in the comments ~ is the bitwise NOT.
If you want a 5 bit unsigned bitwise NOT you can use an XOR with a mask:
>>> n = 0b10010 # 18
>>> m = 0b11111
>>> n ^ m
13

Related

Why complement of 0 is -1? [duplicate]

This question already has answers here:
The tilde operator in Python
(9 answers)
Closed last year.
I was learning about bitwise operators and I learnt that complement of 0 is 1 and 1 is 0. But when I tried using ~0 on IDLE, it printed -1 and when i typed ~1 it gave -2..
-1 is 0-1 => 00..00-00...01 = 1..11
So, as long as you consider some finite width (width is the size of integer or binary form you are using,4,8 ....),it is true that:
00..00 =~11..11
then, the following also be true:
~0=-1

Why in Python 3.6 print(-5 ^ -3) is 6? [duplicate]

This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
Closed 2 years ago.
In the Notebook of Google Colaboratory, whose version is python 3.6 by putting the following syntax - print(-5 ^ -3), I have the result as 6. I want to know why I get this result and how python interprets it.
Edit : This question is not a dupe of other question because that question is just general explanation of bitwise XOR operation and doesn't explain how bitwise operation works with negative numbers ( in python ).
Negative numbers are written with a leading one instead of a leading
zero. So if you are using only 8 bits for your twos-complement
numbers, then you treat patterns from 00000000 to 01111111 as the
whole numbers from 0 to 127, and reserve 1xxxxxxx for writing
negative numbers. (source)
Explanation :
The statement print(-5^-3) will basically perform bitwise-XOR operation of -5 and -3 and print their result. Lets see the binary representation of these two numbers
The binary representation of -5 can be considered as 1000...101 and binary representation of -3 can be considered as 1000...011. Here 1 at the MSB ( Most Significant Bit ) denotes that the number represented by the binary representation is negative.
So Xor will return 1 only if two bits are of opposite ( 1 and 0 ). Here's how xor will work considering 8-bit representation of the numbers.
XOR Operation :
-5 : 10000101
^
-3 : 10000011
----------------------
Result : 00000110
And 00000110 in binary representation is considered as 6 in decimals. Hence, the answer that gets printed is 6.
Hope this helps !

python ** vs math.pow() regarding negative values [duplicate]

This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 6 years ago.
from math import pow
assert pow(-3, 2) == 9
assert -3 ** 2 == -9
why are the two above assertions valid?
in regular math, when a negative numbered is powered to 2, it becomes positive. which one of these is equal to the regular math I know?
is ignoring negative value the only difference between these two methods?
Its because of the order in which the operations are performed. In the first case, pow(-3,2) takes as inputs a -3 as first input and a 2 as the second input. In the second case, the ** has precedence over the -, so the order in which the operations are executed is
Calculate 3**2
Change the sign of the result
This leads to the result being -9.
Because python calculate the minus after it calculate the power.
In [2]: -3**2
Out[2]: -9
In [3]: (-3)**2
Out[3]: 9

What does "e" in "1e-5" in Python language mean and what is the name of this notation? [duplicate]

This question already has answers here:
What does the suffix e+number mean in python at the end of a float?
(2 answers)
What is the meaning of number 1e5?
(5 answers)
Closed 6 years ago.
I notice that there is such an expression "1e-5" in Python(probably in other languages also)
What is the name of this notation?
what does it denote in math?
What does 'e' mean? It's the first time I see a character helps to denote some value, are there other characters also help do so?
Why should use this way instead of some other python math operation like pow() etc.
It is scientific notation. It means 1 × 10−5. In other words, 0.00001.
10 ** -5, i.e. 10 to the power of negative 5, 1 divided by 10 to the power of 5, or 0.00001.
It means 10 to the power of -5 times 1

Python operator ">>" [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python - '>>' operator
What does the >> operator means in Python?
ie:
x = x + str(n%2)
n >> 1
Thank you
n >> 1 shifts n right 1 bit. This is the same as dividing by 2.
More generally, n >> m shifts n right m bits, giving a division by 2^m.
See also:
Shifting operations in Python's documentation
Logical shift on Wikipedia
Just for completeness, note that there's a completely different usage which is changing the stream that print uses by default:
print >> sys.stderr, message
For more information, please have a look at this related question.
That's the right shift operator.
It's the bitshift operator:
x >> n
x shifted right by n bits.

Categories