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
72
votes
11 answers

What USEFUL bitwise operator code tricks should a developer know about?

I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more…
non sequitor
  • 18,296
  • 9
  • 45
  • 64
71
votes
6 answers

warning: left shift count >= width of type

I'm very new to dealing with bits and have got stuck on the following warning when compiling: 7: warning: left shift count >= width of type My line 7 looks like this unsigned long int x = 1 << 32; This would make sense if the size of long on my…
Rupert Madden-Abbott
  • 12,899
  • 14
  • 59
  • 74
69
votes
16 answers

Bitwise operator for simply flipping all bits in an integer?

I have to flip all bits in a binary representation of an integer. Given: 10101 The output should be 01010 What is the bitwise operator to accomplish this when used with an integer? For example, if I were writing a method like int flipBits(int…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
69
votes
12 answers

Is shifting bits faster than multiplying and dividing in Java? .NET?

Shifting bits left and right is apparently faster than multiplication and division operations on most, maybe even all, CPUs if you happen to be using a power of 2. However, it can reduce the clarity of code for some readers and some algorithms. Is…
user132748
68
votes
5 answers

Understanding PHP & (ampersand, bitwise and) operator

I often use ($var & 1) in my code, which returns true if $var is an odd number and false if it's an even number. But what does "&" actually do?
ryonlife
  • 6,563
  • 14
  • 51
  • 64
68
votes
0 answers

What is a bitmask and a mask?

On the PHP documentation about JSON it mentions the word bitmask. Wikipedia has it defined as a mask. I don't understand either bitmask or mask or how they are useful. Can some one explain these terms using layman's terms and no jargon?
Robert
  • 10,126
  • 19
  • 78
  • 130
67
votes
18 answers

get absolute value without using abs function nor if statement

I was thinking how to get the absolute value of an integer without using if statement nor abs(). At first I was using shift bits left (<<), trying to get negative sign out of the range, then shift bits right back to where it be, but unfortunately it…
shanwu
  • 1,493
  • 6
  • 35
  • 45
67
votes
9 answers

What does a bitwise shift (left or right) do and what is it used for?

I've seen the operators >> and << in various code that I've looked at (none of which I actually understood), but I'm just wondering what they actually do and what some practical uses of them are. If the shifts are like x * 2 and x / 2, what is the…
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161
67
votes
13 answers

Implement division with bit-wise operator

How can I implement division using bit-wise operators (not just division by powers of 2)? Describe it in detail.
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
66
votes
9 answers

Algorithm to generate bit mask

I was facing this unique problem of generating a bit-mask based on the input parameter. For example, if param = 2, then the mask will be 0x3 (11b) if param = 5, then the mask will be 0x1F (1 1111b) This I implemented using a for-loop in C,…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
66
votes
9 answers

Fast computing of log2 for 64-bit integers

A great programming resource, Bit Twiddling Hacks, proposes (here) the following method to compute log2 of a 32-bit integer: #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n static const char LogTable256[256] = { -1, 0, 1, 1, 2, 2,…
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
63
votes
3 answers

Is there an elegant way to Invert a Bit value in an SQL insert Statement?

I'm converting some data in SQL Server: INSERT INTO MYTABLE (AllowEdit) (Select PreventEdit from SOURCETABLE) so I need to inverse the bit value from source table. I expected NOT to work, as this is how I would do it in code, but it doesn't. The…
Molloch
  • 2,261
  • 4
  • 29
  • 48
61
votes
7 answers

Android In App Billing: securing application public key

From Android In App Billing version 3 (TrivialDrive)sample application coming with sdk MainActivity.java /* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY * (that you got from the Google Play developer console). This is not your *…
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
61
votes
15 answers

Two's Complement Binary in Python?

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…
Thor Correia
  • 1,528
  • 3
  • 23
  • 30
60
votes
11 answers

Fast way to generate pseudo-random bits with a given probability of 0 or 1 for each bit

Normally, a random number generator returns a stream of bits for which the probability to observe a 0 or a 1 in each position is equal (i.e. 50%). Let's call this an unbiased PRNG. I need to generate a string of pseudo-random bits with the following…
o9000
  • 1,626
  • 13
  • 15