0

I've just started the "advanced" stages of the Python 2.7 course on Codeacademy and went to the extra effort of trying to write a function to perform a manual bitwise OR (|) operation.

What I came up with is not the most readable solution (so not so Pythonic)...

def bitwiseOr(bin1, bin2):
    """Perform a bitwise OR swap of two string representations of a binary number"""

    # if the second bit is larger (in length), swap them
    if len(bin1) < len(bin2):
        bin1, bin2 = bin2, bin1

    # cast string into list using list comprehension
    result = [x for x in bin1]
    resultString = ""

    # start at the end of the smallest length bit string, 
    # moving backwards by 1 char, and stop at the 2nd char
    for i in range(len(bin2), 2, -1):        
        if bin2[i-1] == "1":
            result[i] = bin2[i-1]

    # convert the list back into a string
    for item in result:
        resultString += item        

    return resultString


print bin(0b1110 | 0b101)
print bitwiseOr("0b101", "0b1110")

The above two print calls both return the same result (albeit with the first call returning a binary number and the second returning a string representation of a binary number).

Readability aside - I'm interested to see how it's logically done, underneath the hood, by Python internally. Poking around in the CPython repository didn't yield much, despite the fact that I think I found the right file (C is very foreign to me).

What I mean by logically is, there are normally a few ways to solve any given problem, and I solved this one by passing the base 2 representations of each number as strings, creating a list based on the larger length binary, and comparing each character, preferencing the 1.

How does Python do it internally?

GrayedFox
  • 2,350
  • 26
  • 44
  • I'm asking "by what logic does Python swap or replace each bit". I understand *what* it does, now I want to know **how**. – GrayedFox Oct 07 '16 at 17:01
  • I am pretty sure that in the end it just translates it into a low level CPU instruction. – gre_gor Oct 07 '16 at 17:02
  • So you're asking how the bits are replaced? Take `100101` as an example. `100101 | 011101` is `111101`. If either bit is 1, then the resulting bit is 1. – Andrew Li Oct 07 '16 at 17:03
  • 2
    Yeah, the CPU understands bitwise operations. Python doesn't have to do anything with them, execpt maybe chop up long bitstrings – Patrick Haugh Oct 07 '16 at 17:03
  • Hmm... I think perhaps I didn't phrase my question correctly. You see, inside the OP, I have defined function which - logically - outputs the same results as the Python Bitwise OR. I want to know how Python implements the swap logic - that is, what does it look like in C, and *how*, logically, does it do it's job? – GrayedFox Oct 12 '16 at 08:25
  • @PatrickHaugh you actually answered my question properly! I didn't realise the CPU itself understood the bitwise operation. Thank you – GrayedFox Oct 17 '16 at 17:26

2 Answers2

1

The OR operator is like a parallel electrical circuit with two routes, even if one of the routes is broken the current will still flow through. It only stops when both the routes are broken. But you have to be careful with an OR operator in python, although it looks simple the logic has to be decided really carefully or else you might have a very hard time debugging your code.

Hammad Manzoor
  • 144
  • 1
  • 11
  • This is a good analogy however doesn't quite answer the question, which I've now updated to be clearer. But upvoted, thank you – GrayedFox Oct 12 '16 at 08:38
0

This question, to the untrained eye (someone without a computer science background), is about trying to understand the C implementation of Python's bitwise OR operation.

However after some more research I've realised the question, to the trained eye, might seem absurd, since it is actually the processor itself which understands this type of operation.

What this means is that the internal logic of the Python bitwise OR operation is actually entirely dependent on the instructions available to the CPU, that is, it is not dependent on a higher level language.

So in summary, the internal logic of the Python bitwise OR operator performs a comparison on the bits (the 1's and 0's) of the machine in question, and how it performs this operation is a direct comparison of said bits which, mind-blowingly, is not dissimilar to how Ancient Egyptians performed multiplication. Woah.

GrayedFox
  • 2,350
  • 26
  • 44