November 10, 2014

Week of 11/02/14 - Python Part 09: Difficulty

    Now we're doing hard stuff. I started out this week by doing more with lambda expressions and functional programming. I have it sort-of figured out, but I still don't understand them completely. Apparently they are very good for filtering lists. I also searched on wikipedia on how functional programming works, but I just got a little lost on lambda calculus.

    Then I moved on to bitwise operators, which are operations on binary numbers (leftshift, rightshift, OR, AND, NOT, and XOR). Leftshift and rightshift are just like how when you multiply by ten in the decimal system, you just add a zero on the end. This is the same in binary, except it's just base 2. S it's also basically appending a zero at the end for leftshift, while rightshifting requires that you remove the last number. Left shift is denoted with the operator <<, while >> denotes rightshift.
    The OR operator is another operator which requires 2 binary numbers, separated by a "|". What it does is it takes the ones digit of the each binary number and compares them. If both of them are 0, it returns a 0 to the ones digit of the output number. However, if any or both of them are 1, then it returns a 1 to the ones digit. Then it does the same thing to the next digit. For example, we have the binary numbers 00101011 and 11111001, and we will be OR-ing them together.
     0 0 1 0 1 0 1 [1]
OR   1 1 1 1 1 1 0 [1]


    In the ones digit, both of them are 1, so in the output, there is going to be a 1.
     0 0 1 0 1 0 [1] 1
OR   1 1 1 1 1 0 [0] 1
    ___________________
                     1

    In the next digit over, we have a 1 and a 0. In the output, the next digit is going to be a 1.
     0 0 1 0 1 [0] 1 1
OR   1 1 1 1 1 [0] 0 1
    ___________________
                   1 1

    Next digit, we have a 0 and a 0. In the output, it would have a 0.
      0 0 1 0 1 [0] 1 1
OR    1 1 1 1 1 [0] 0 1
    ___________________
                  0 1 1

    You would just keep doing this until all of the digits are exhausted. In the end, the output number would be 1111011.

I think I'll explain the other bitwise operators next week.

No comments: