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
Related
This question already has answers here:
What is “two's complement”?
(24 answers)
How to think about Python's negative number bitwise operations?
(2 answers)
Closed 1 year ago.
I have a question in my programming class, it is: What is the value of ~12 and the answer is -13. I don't understand why? I convert 13 base 10 to binary, which is 1100, than I switched all the 1 for 0 and vice versa, it gives me 0011, so I thought the answer was 3 but it's not.
Converting 12 to a signed binary number yields 01100 then assuming that ~ is supposed to invert every bit we get 10011 which can be converted from signed binary to decimal yielding -13.
This question already has answers here:
Converting bitstring to 32-bit signed integer yields wrong result
(3 answers)
Closed 2 years ago.
Is there a way to force python to treat a number based upon its sign?
E.g. 0xFFFFFFFF = -1 instead of 4294967295?
You can use ctypes.c_int32 for a signed 32 bit integer:
import ctypes
wrapped = ctypes.c_int32(0xFFFFFFFF)
print(wrapped) # c_int(-1)
print(wrapped.value) # -1
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 !
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
This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10