November 12, 2014

Week of 11/09/14 - Python Part 10: More Bitwise Operators

    Seeing as last week I worked on the OR operator, I'm going to be working on explaining AND, NOT, and XOR, and maybe classes if I get around to it.

    The AND operator (&), like the OR operator, requires 2 input binary numbers, and the inputs are operated on on a digit by digit basis. Let's say you have the binary numbers 00101011 and 11111001 again. This time, only if both numbers are 1s is the output 1, while the rest are zeros. For example, the first digits are 1 and 1, respectively. Therefore, the output's first digit is also a 1. However, the next digits are a 1 and a 0 respectively. The next digit in the output is a 0. So far we have ??????01 as our output. Next digit we have a 0 from both numbers. The output of that would also be a 0. Continuing this, we end up with 00101001.

    XOR (^) is basically like the AND operator, except it also outputs a zero when both inputs are zeroes. So 00101011 XOR 11111001 ends up as 00101101.

   The NOT operator is special because it requires only one input number. I will also have to differentiate between signed and unsigned binary integers.

    Using unsigned integers, the NOT (~) of an integer is basically all of its digits, just flipped. So every 1 becomes a 0, and every 0 becomes a 1. For example, we have the binary number 01010101, whose decimal representation is 85. It's NOT is 10101010, and it's decimal representation would be 170. However, this is an unsigned integer, meaning the system that it's using can't differentiate between negative and positive numbers. That's why we have signed integers. Python uses the "Two's Complement" method to represent negative numbers in binary. This means that while 01111111 represents 127, it's NOT, which is 10000000, would be considered to be -128 in the "Two's Complement" system. This means when you NOT a decimal integer in Python, the output is the negative of that number minus 1. So NOT 45 is -46, NOT 85 is -86, and NOT 127 is -128 (I read this off of a forum).


    Now, I am learning about what classes are. I don't really understand them, but they seem to organize objects and gives them attributes. It also says that classes are very important to object-oriented programming. The only familiarity I have with classes and programming are all of the .class files found within a java executable (.jar), but I'm pretty sure those are something different.