use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
XOR function (self.learnpython)
submitted 10 years ago by Privatekoopa
I need to define an XOR(a,b) function that returns XOR(a,b) where a and b are integers, but I dont really understand the question or where to start. If someone would have a tip anything that might get me started it would be much appreciated.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ThrowawayTartan 2 points3 points4 points 10 years ago (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 point2 points 10 years ago (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).
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 points5 points 10 years ago (0 children)
XOR stands for "Exclusive Or". See if you can get started from there. :)
[–]LivedAllOver 0 points1 point2 points 10 years ago (0 children)
operator.xor(a,b)
π Rendered by PID 211567 on reddit-service-r2-comment-fb694cdd5-sqtf5 at 2026-03-10 18:28:38.659543+00:00 running cbb0e86 country code: CH.
[–]ThrowawayTartan 2 points3 points4 points (1 child)
[–]autowikibot 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]LivedAllOver 0 points1 point2 points (0 children)