What is a more efficient way to perform bitwise operations in Python?

In summary, The conversation discusses the implementation of bitwise operations in Python without using built-in operators. The two functions provided, bitwise_and and divmod_and, demonstrate different methods of performing the operations and converting the result to an integer. The conversation also includes a suggestion to use the divmod operator for a simpler implementation.
  • #1
Adyssa
203
3
I'm playing around in Python at the moment, and I came across an exercise to perform bitwise operations manually (without the built in & | ^ ~ operators).

I understand the operations on paper, and I have a function here that performs them (& in this case, the others are similar):

Code:
def bitwise_and(num1, num2):

	result = "" # the result of the bitwise and operation

	while num1 > 0 or num2 > 0:
		# num % 2  != 0 implies a 1-bit in the lowest position
		if num1 % 2 != 0 and num2 % 2 != 0: 
			result = "1" + result # both bits are 1, so 1 & 1 == 1, append 1 to result
		else:
			result = "0" + result# at least one 0 bit, 1 & 0 == 0, 0 & 0 == 0, append 0

		num1 = num1 >> 1 # drop the last bit
		num2 = num2 >> 1 # drop the last bit

	return int(result,2) # return an int representation of the result binary string

I'd like some help modifying this implementation to use an integer type for the result instead of a string type which I then have to convert. I was trying to set the current result bit and then left shift (<<) but it didn't work when I only had 0 bits to work with because 0b0 == 0b00 == 0b0000000 so there's no significant 1 bit to shift. Is there a neater way do this?
 
Last edited:
Technology news on Phys.org
  • #2


I've not programmed in Python, however, ... Have you considered using the divmod operator, with the 'div' bit providing the shifted number and then multiplying the 'mod' bits together (then iterating until the 'div' bit of either number is 0). It seems to work in Mathcad ...

attachment.php?attachmentid=54739&stc=1&d=1358249555.jpg
 

Attachments

  • phys - 13 01 15 boolean 01.jpg
    phys - 13 01 15 boolean 01.jpg
    33.2 KB · Views: 854
  • #3


Thanks, that's nice and simple :) I can stare at problems forever and just not grok things properly sometimes! Here's some Python code:

Code:
#divmod version

def divmod_and(x, y):
    z = 0
    p = 1
    while x or y:
        x, xd = divmod(x, 2)
        y, yd = divmod(y, 2)
        z = p * xd * yd + z
        p <<= 1

    return z

# manual mod and divide for clarity

def bitwise_and(x, y):
    result = 0
    power = 1
    while x or y:
        bit = x % 2 * y % 2 # both bits on == 1
        result = bit * power + result
        x /= 2
        y /= 2
        power <<= 1

    return result
 

1. What are bitwise operations in Python?

Bitwise operations in Python are used to manipulate individual bits in binary numbers. These operations include AND, OR, XOR, NOT, and bit shifting.

2. How do I perform a bitwise operation in Python?

To perform a bitwise operation in Python, you can use the following operators: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).

3. What is the purpose of using bitwise operations in Python?

The purpose of using bitwise operations in Python is to manipulate binary data more efficiently and perform low-level operations, such as checking and setting individual bits.

4. Can I use bitwise operations on non-integer data types in Python?

No, bitwise operations can only be performed on integer data types in Python. Trying to use them on non-integer data types will result in an error.

5. Are bitwise operations reversible in Python?

Some bitwise operations, such as XOR, are reversible in Python. However, other operations such as AND and OR are not reversible. This means that the original input cannot be obtained from the output of the operation.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
8
Views
7K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
874
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
7
Views
429
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top