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
190
votes
9 answers

Why is XOR the default way to combine hashes?

Say you have two hashes H(A) and H(B) and you want to combine them. I've read that a good way to combine two hashes is to XOR them, e.g. XOR( H(A), H(B) ). The best explanation I've found is touched briefly here on these hash function…
Nate Murray
  • 3,841
  • 5
  • 32
  • 33
186
votes
8 answers

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

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, …
rysqui
  • 2,779
  • 2
  • 19
  • 26
182
votes
17 answers

What does (x ^ 0x1) != 0 mean?

I came across the following code snippet if( 0 != ( x ^ 0x1 ) ) encode( x, m ); What does x ^ 0x1 mean? Is this some standard technique?
KodeWarrior
  • 3,538
  • 3
  • 26
  • 40
177
votes
11 answers

Are the shift operators (<<, >>) arithmetic or logical in C?

In C, are the shift operators (<<, >>) arithmetic or logical?
littlebyte
  • 1,789
  • 2
  • 11
  • 5
174
votes
3 answers

How can I remove a flag in C?

There is a variable that holds some flags and I want to remove one of them. But I don't know how to remove it. Here is how I set the flag. my.emask |= ENABLE_SHOOT;
Aaron de Windt
  • 16,794
  • 13
  • 47
  • 62
157
votes
32 answers

What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?

If I have some integer n, and I want to know the position of the most significant bit (that is, if the least significant bit is on the right, I want to know the position of the farthest left bit that is a 1), what is the quickest/most efficient…
Zxaos
  • 7,791
  • 12
  • 47
  • 61
153
votes
11 answers

~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 like there should be some number that satisfies the conditions. I tried testing the numbers between -5000 and 5000 but never…
Steve
  • 4,446
  • 7
  • 35
  • 50
152
votes
42 answers

In C/C++ what's the simplest way to reverse the order of bits in a byte?

While there are multiple ways to reverse bit order in a byte, I'm curious as to what is the "simplest" for a developer to implement. And by reversing I mean: 1110 -> 0111 0010 -> 0100 This is similar to, but not a duplicate of this PHP…
nathan
  • 5,513
  • 4
  • 35
  • 47
146
votes
10 answers

What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa?

I was solving some problem on codeforces. Normally I first check if the character is upper or lower English letter then subtract or add 32 to convert it to the corresponding letter. But I found someone do ^= 32 to do the same thing. Here it is:…
Devon
  • 1,253
  • 2
  • 7
  • 4
144
votes
23 answers

Position of least significant bit that is set

I am looking for an efficient way to determine the position of the least significant bit that is set in an integer, e.g. for 0x0FF0 it would be 4. A trivial implementation is this: unsigned GetLowestBitPos(unsigned value) { assert(value != 0);…
peterchen
  • 40,917
  • 20
  • 104
  • 186
139
votes
3 answers

Why does this random value have a 25/75 distribution instead of 50/50?

Edit: So basically what I'm trying to write is a 1 bit hash for double. I want to map a double to true or false with a 50/50 chance. For that I wrote code that picks some random numbers (just as an example, I want to use this on data with…
gvlasov
  • 18,638
  • 21
  • 74
  • 110
130
votes
8 answers

How do I get bit-by-bit data from an integer value in C?

I want to extract bits of a decimal number. For example, 7 is binary 0111, and I want to get 0 1 1 1 all bits stored in bool. How can I do so? OK, a loop is not a good option, can I do something else for this?
Badr
  • 10,384
  • 15
  • 70
  • 104
130
votes
15 answers

Should I use #define, enum or const?

In a C++ project I'm working on, I have a flag kind of value which can have four values. Those four flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each…
Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
126
votes
16 answers

Best practices for circular shift (rotate) operations in C++

Left and right shift operators (<< and >>) are already available in C++. However, I couldn't find out how I could perform circular shift or rotate operations. How can operations like "Rotate Left" and "Rotate Right" be performed? Rotating right…
Elroy
  • 605
  • 4
  • 12
  • 20
126
votes
22 answers

C/C++ check if one bit is set in, i.e. int variable

int temp = 0x5E; // in binary 0b1011110. Is there such a way to check if bit 3 in temp is 1 or 0 without bit shifting and masking. Just want to know if there is some built in function for this, or am I forced to write one myself.
Milan
  • 15,389
  • 20
  • 57
  • 65