Questions tagged [bit-manipulation]

The manipulation of individual bits. Operators used may include bitwise AND, OR, XOR, NOT, left-shift, and right-shift.

The manipulation of individual bits. Operators used may include bitwise AND (&), OR (|), XOR (^), NOT (~), left-shift (<<), and right-shift (>>).

A blog article, Introduction to Low Level Bit Hacks, starts with basics like x |= 1<<n for setting a bit, then breaks down interesting ones like y = x & (x-1) to clear the rightmost set bit (like x86 blsr), with detailed examples showing binary values of intermediates.

Sean Anderson has a large collection of problems which can be solved efficiently by bitwise operations; the collection can be found here.

Alan Mycroft has a somewhat smaller collection of (mostly bit-twiddling) programming hacks, mostly transposed from HAKMEM. It's available here.

7844 questions
53
votes
7 answers

Efficient way to OR adjacent bits in 64-bit integer

What I want to do is take a 64-bit unsigned integer consisting of pairs of bits and create from it a 32-bit integer containing 0 if both bits in the corresponding pair are 0 and 1 otherwise. In other words, convert something that looks like : 01 00…
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
52
votes
5 answers

Checking flag bits java

I have a problem with flag bits. I have an int variable to hold flags. First I set some flags to that variable. Later I need check how many flags were set in that variable. But I don't know to do it.
Nagaraju
  • 1,205
  • 2
  • 11
  • 12
52
votes
7 answers

Unsigned Integer in Javascript

I'm working on a page that processes IP address information, but it's choking on the fact that integers are signed. I am using bitwise operators to speed it up, but the 64th bit (signed/unsigned flag) is messing it up. Is there any way to force a…
bradlis7
  • 3,375
  • 3
  • 26
  • 34
52
votes
18 answers

Bitfield manipulation in C

The classic problem of testing and setting individual bits in an integer in C is perhaps one the most common intermediate-level programming skills. You set and test with simple bitmasks such as unsigned int mask = 1<<11; if (value & mask) {....}…
SPWorley
  • 11,550
  • 9
  • 43
  • 63
51
votes
4 answers

Understanding the bitwise AND Operator

I have been reading about bit operators in Objective-C in Kochan's book, "Programming in Objective-C". I am VERY confused about this part, although I have really understood most everything else presented to me thus far. Here is a quote from the…
Qcom
  • 18,263
  • 29
  • 87
  • 113
51
votes
3 answers

How does this work? Weird Towers of Hanoi Solution

I was lost on the internet when I discovered this unusual, iterative solution to the towers of Hanoi: for (int x = 1; x < (1 << nDisks); x++) { FromPole = (x & x-1) % 3; ToPole = ((x | x-1) + 1) % 3; moveDisk(FromPole,…
Andres
  • 5,012
  • 3
  • 24
  • 36
49
votes
5 answers

Mod of power 2 on bitwise operators?

How does mod of power of 2 work on only lower order bits of a binary number (1011000111011010)? What is this number mod 2 to power 0, 2 to power 4? What does power of 2 have to do with the modulo operator? Does it hold a special property? Can…
Zo Has
  • 12,599
  • 22
  • 87
  • 149
48
votes
13 answers

Convert 0x1234 to 0x11223344

How do I expand the hexadecimal number 0x1234 to 0x11223344 in a high-performance way? unsigned int c = 0x1234, b; b = (c & 0xff) << 4 | c & 0xf | (c & 0xff0) << 8 | (c & 0xff00) << 12 | (c & 0xf000) << 16; printf("%p -> %p\n", c,…
exebook
  • 32,014
  • 33
  • 141
  • 226
47
votes
6 answers

(x | y) - y why can't it simply be x or even `x | 0`

I was reading a kernel code, and in one place I've seen an expression inside if statement like if (value == (SPINLOCK_SHARED | 1) - 1) { ............ } where SPINLOCK_SHARED = 0x80000000 is a predefined constant. I wonder why do we need…
RaGa__M
  • 2,550
  • 1
  • 23
  • 44
47
votes
14 answers

Fastest way to clamp a real (fixed/floating point) value?

Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be…
Niklas
  • 5,736
  • 7
  • 35
  • 42
47
votes
17 answers

Find most significant bit (left-most) that is set in a bit array

I have a bit array implementation where the 0th index is the MSB of the first byte in an array, the 8th index is the MSB of the second byte, etc... What's a fast way to find the first bit that is set in this bit array? All the related solutions I…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
47
votes
5 answers

How can I detect integer overflow on 32 bits int?

I know such topic was asked several times, but my question is about overflow on full 32 bits of int. For example: 11111111111111111111111111111111 + 00000000000000000000000000000001 = 00000000000000000000000000000000 //overflow! I found…
ashur
  • 4,177
  • 14
  • 53
  • 85
46
votes
8 answers

Performance wise, how fast are Bitwise Operators vs. Normal Modulus?

Does using bitwise operations in normal flow or conditional statements like for, if, and so on increase overall performance and would it be better to use them where possible? For example: if(i++ & 1) { } vs. if(i % 2) { }
Maven
  • 14,587
  • 42
  • 113
  • 174
46
votes
13 answers

Reversing bits of Python integer

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…
David Chouinard
  • 6,466
  • 8
  • 43
  • 61
46
votes
5 answers

Bitwise XOR of hexadecimal numbers

How can we XOR hexadecimal numbers in Python? For example, I want to XOR 'ABCD' and '12EF', the answer should be 'B922'. I used the code below, but it gives the wrong results. # xor two strings of different lengths def strxor(a, b): if len(a) >…
Pratibha
  • 1,730
  • 7
  • 27
  • 46