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

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 !

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

How ~12 gives -13 in decimal? [duplicate]

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.

How to check what the last binary digit of a variable is? [duplicate]

This question already has answers here:
Python int to binary string?
(36 answers)
Check if a number is odd or even in Python [duplicate]
(6 answers)
Closed 5 years ago.
So I'm building various even-odd checkers. Standard method using python - x%2 == 0 as a boolean. Fine.
However, I've read that a different way of checking is to grab the binary string representing the number, then simply check the last digit of the string. If the last digit is 0, it's even, if it's 1, it's odd. For example:
00001011 = 11
Last digit is 1
11 is odd
00001110 = 14
Last digit is 0
14 is even.
It would amuse me to no end to be able to build even/odd checkers that use the binary string instead of a %2 check. The only thing I can't figure out, and my google-fu is failing me - how to grab and handle the string?
Threads for reference: Compare the last binary digit to 1 in MIPS assembly gives an assembly answer.
All of the other threads deal with getting the last digit of a normal number, IE last digit of 12345 is 5, and ignore the "How to read it in binary"
Try this:
def is_even(n):
if "{0:b}".format(n).endswith(0): # Checks if converted number ends with 0
return True
else:
return False

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

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

How python calculate this division? [duplicate]

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

Categories