Top "Bit-manipulation" questions

The manipulation of individual bits.

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the …

python bit-manipulation primes bitwise-operators logarithm
invert specific bits using bitwise NOT

does anyone have experience to use Bitwise Not to invert specific bits for x number. I know we can do …

c bit-manipulation invert
How to convert bitarray to an integer in python

Suppose I define some bitarray in python using the following code: from bitarray import bitarray d=bitarray('0'*30) d[5]=1 …

python bit-manipulation bitarray
Bash shell, trying to create and evaluate a mask

I'm trying to create a mask and use the bitwise operator "&" to compare to another variable and see the …

bash shell bit-manipulation mask bitmask
I want to calculate the inverse mask for an unsigned char

I would like to calculate an inverse mask for an unsigned char.meaning if the original mask 0xc0 the the …

c bit-manipulation bitmask
Absolute value abs(x) using bitwise operators and Boolean logic

How does this work? The idea is to make abs(x) use bitwise operators for integers (assuming 32 bit words): y = …

c++ c bit-manipulation language-lawyer absolute-value
How do I write a maintainable, fast, compile-time bit-mask in C++?

I have some code that is more or less like this: #include <bitset> enum Flags { A = 1, B = 2, C = 3, …

c++ c++11 bit-manipulation
~x + ~y == ~(x + y) is always false?

Does this code always evaluate to false? Both variables are two's complement signed ints. ~x + ~y == ~(x + y) I feel …

c bit-manipulation signed twos-complement
Writing files in bit form to a file in C

I am implementing the huffman algorithm in C. I have got the basic functionality down up to the point where …

c bit-manipulation binaryfiles huffman-code
Why is abs(0x80000000) == 0x80000000?

I just started reading Hacker's Delight and it defines abs(-231) as -231. Why is that? I tried printf("%x", …

c integer bit-manipulation 32-bit