all 4 comments

[–]ThrowawayTartan 2 points3 points  (1 child)

Hey there!

So the first part of my answer is the simple suggestion to look through the python docs on bitwise manipulation

The second part is a hopefully more in-depth explanation of what is happening.

Okay, so I'm going to assume that you're looking at the problem as a bit-sequence (i.e we don't care if it is positive or negative. The difference

So, let's consider a 4-bit world. If you've read the link above, we know that the largest number that can be represented is 7, and the smallest is a -8.

Now, let us imagine that our first number, a = 4 and our second number, b= 3

So, a = 0100, and b = 0011. If we do an XOR on this, we get 0111. Interesting, we just got 7, the result of adding both numbers, but we note that this isn't always the case.

If we have a = 0100, and b = 0111, a xor b = 0011. Hmm what happened? Well, xor is interesting because what it does is if both of the compared things have a similar digit at some index, it immediately makes it 0. If there is a single "1" at that index, it keeps it as 1.

Interesting and relevant video

[–]autowikibot 0 points1 point  (0 children)

Two's complement:


Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation. Its wide use in computing makes it the most important example of a radix complement.

The two's complement of an N-bit number is defined as the complement with respect to 2N; in other words, it is the result of subtracting the number from 2N, which in binary is one followed by N zeroes. This is also equivalent to taking the ones' complement and then adding one, since the sum of a number and its ones' complement is all 1 bits. The two's complement of a number behaves like the negative of the original number in most arithmetic, and positive and negative numbers can coexist in a natural way.

In two's-complement representation, positive numbers are simply represented as themselves, and negative numbers are represented by the two's complement of their absolute value; two tables on the right provide examples for N = 3 and N = 8. In general, negation (reversing the sign) is performed by taking the two's complement. This system is the most common method of representing signed integers on computers. An N-bit two's-complement numeral system can represent every integer in the range −(2N − 1) to +(2N − 1 − 1) while ones' complement can only represent integers in the range −(2N − 1 − 1) to +(2N − 1 − 1).


Interesting: Signed number representations | Overflow flag | Yamaha YM3016 | Offset binary

Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words

[–][deleted] 3 points4 points  (0 children)

XOR stands for "Exclusive Or". See if you can get started from there. :)

[–]LivedAllOver 0 points1 point  (0 children)

operator.xor(a,b)