bit-manipulation

Infinite loop while adding two integers using bitwise operations?

Infinite loop while adding two integers using bitwise operations? Question: I am trying to solve a problem, using python code, which requires me to add two integers without the use of ‘+’ or ‘-‘ operators. I have the following code which works perfectly for two positive numbers: def getSum(self, a, b): while (a & b): …

Total answers: 3

Applications of '~' (tilde) operator in Python

Applications of '~' (tilde) operator in Python Question: I just discovered the bitwise complement unary operation in Python via this question and have been trying to come up with an actual application for it, and if not, to determine if it’s generally safe to overload the operator (by overriding the __invert__ method) for other uses. …

Total answers: 7

Is unsetting a single bit in flags safe with Python variable-length integers?

Is unsetting a single bit in flags safe with Python variable-length integers? Question: In my program (written in Python 3.4) I have a variable which contains various flags, so for example: FLAG_ONE = 0b1 FLAG_TWO = 0b10 FLAG_THREE = 0b100 status = FLAG_ONE | FLAG_TWO | FLAG_THREE Setting another flag can easily be done with …

Total answers: 2

A + B without arithmetic operators, Python vs C++

A + B without arithmetic operators, Python vs C++ Question: I was trying to solve an old question: Write a function that add two [integer] numbers A and B. You should not use + or any arithmetic operators. The best solution is like this, quoted from “LintCode-A+B Problem“: For a + b in any base, …

Total answers: 6

'and' (boolean) vs '&' (bitwise) – Why difference in behavior with lists vs numpy arrays?

'and' (boolean) vs '&' (bitwise) – Why difference in behavior with lists vs numpy arrays? Question: What explains the difference in behavior of boolean and bitwise operations on lists vs NumPy arrays? I’m confused about the appropriate use of & vs and in Python, illustrated in the following examples. mylist1 = [True, True, True, False, …

Total answers: 8

What is a Readable/Modern Way to Parse a Bit-Based Error Code?

What is a Readable/Modern Way to Parse a Bit-Based Error Code? Question: I’m tasked with reading error codes from printers via snmp. Luckily, I have a working bash script to guide me through this arcane task. I’m writing some python to do some different work from the existing script. The existing code seems to work, …

Total answers: 3

Two's Complement Binary in Python?

Two's Complement Binary in Python? Question: Integers in Python are stored in two’s complement, correct? Although: >>> x = 5 >>> bin(x) 0b101 And: >>> x = -5 >>> bin(x) -0b101 That’s pretty lame. How do I get python to give me the numbers in REAL binary bits, and without the 0b infront of it? …

Total answers: 15

Reversing bits of Python integer

Reversing bits of Python integer Question: Given a decimal integer (eg. 65), how does one reverse the underlying bits in Python? i.e.. the following operation: 65 → 01000001 → 10000010 → 130 It seems that this task can be broken down into three steps: Convert the decimal integer to binary representation Reverse the bits Convert …

Total answers: 14

Bits list to integer in Python

Bits list to integer in Python Question: I have such list in Python: [1,0,0,0,0,0,0,0]. Can I convert it to integer like as I’ve typed 0b10000000 (i.e. convert to 128)? I need also to convert sequences like [1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0] to integers (here it will return 0b1100000010000000, i.e. 259). Length of list is always a multiple of 8, …

Total answers: 7

Bit masking in Python

Bit masking in Python Question: I have a byte (from some other vendor) where the potential bit masks are as follows: value1 = 0x01 value2 = 0x02 value3 = 0x03 value4 = 0x04 value5 = 0x05 value6 = 0x06 value7 = 0x40 value8 = 0x80 I can count on ONE of value1 through value6 being …

Total answers: 5