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
116
votes
3 answers

What is CHAR_BIT?

Quoting the code for computing the integer absolute value (abs) without branching from http://graphics.stanford.edu/~seander/bithacks.html: int v; // we want to find the absolute value of v unsigned int r; // the result goes here int…
user466534
115
votes
6 answers

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 enum Flags { A = 1, B = 2, C = 3, D = 5, E = 8, F = 13, G = 21, H, I, J, K, L, M, N, O }; void apply_known_mask(std::bitset<64> &bits) { const Flags…
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
112
votes
17 answers

Bitwise operation and usage

Consider this code: x = 1 # 0001 x << 2 # Shift left 2 bits: 0100 # Result: 4 x | 2 # Bitwise OR: 0011 # Result: 3 x & 1 # Bitwise AND: 0001 # Result: 1 I can understand the arithmetic operators in Python (and other…
eozzy
  • 66,048
  • 104
  • 272
  • 428
110
votes
16 answers

How can I test whether a number is a power of 2?

I need a function like this: // return true if 'n' is a power of 2, e.g. // is_power_of_2(16) => true // is_power_of_2(3) => false bool is_power_of_2(int n); Can anyone suggest how I could write this?
Ant
  • 1,111
  • 3
  • 9
  • 5
109
votes
15 answers

How can I multiply and divide using only bit shifting and adding?

How can I multiply and divide using only bit shifting and adding?
Spidfire
  • 5,433
  • 6
  • 28
  • 36
99
votes
4 answers

How do you set, clear and toggle a single bit in JavaScript?

How to set, clear, toggle and check a bit in JavaScript?
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
97
votes
7 answers

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { …
dewald
  • 5,133
  • 7
  • 38
  • 42
94
votes
1 answer

Precedence and bitmask operations

I've come across a (seemingly) very strange case. Take the number 2 (0b10) and bitmask it with 1 (0b01) This should produce 0b00 which is equivalent to 0. However, here's where Mr Schrödinger comes in: var_dump(0b10 & 0b01); // int(0) var_dump(0b10…
94
votes
14 answers

Catch and compute overflow during multiplication of two large integers

I am looking for an efficient (optionally standard, elegant and easy to implement) solution to multiply relatively large numbers, and store the result into one or several integers : Let say I have two 64 bits integers declared like this : uint64_t a…
Ben
  • 7,372
  • 8
  • 38
  • 46
93
votes
5 answers

Manipulate alpha bytes of Java/Android color int

If I have an int in Java that I'm using as an Android color (for drawing on a Canvas), how do I manipulate just the alpha component of that int? For example, how can I use an operation to do this: int myOpaqueColor = 0xFFFFFF; float factor = 0; int…
jnpdx
  • 45,847
  • 6
  • 64
  • 94
91
votes
20 answers

Two's Complement in Python

Is there a built in function in python which will convert a binary string, for example '111111111111', to the two's complement integer -1?
Jim
  • 911
  • 1
  • 7
  • 3
90
votes
10 answers

Is there an elegant and fast way to test for the 1-bits in an integer to be in a contiguous region?

I need to test whether the positions (from 0 to 31 for a 32bit integer) with bit value 1 form a contiguous region. For example: 00111111000000000000000000000000 is contiguous 00111111000000000000000011000000 is not contiguous I want this…
Walter
  • 44,150
  • 20
  • 113
  • 196
88
votes
5 answers

How do I flip a bit in SQL Server?

I'm trying to perform a bitwise NOT in SQL Server. I'd like to do something like this: update foo set Sync = NOT @IsNew Note: I started writing this and found out the answer to my own question before I finished. I still wanted to share with the…
Even Mien
  • 44,393
  • 43
  • 115
  • 119
87
votes
5 answers

bit-wise operation unary ~ (invert)

I'm a little confused by the ~ operator. Code goes below: a = 1 ~a #-2 b = 15 ~b #-16 How does ~ do work? I thought, ~a would be something like: 0001 = a 1110 = ~a why not?
Alcott
  • 17,905
  • 32
  • 116
  • 173
87
votes
11 answers

Saturating subtract/add for unsigned bytes

Imagine I have two unsigned bytes b and x. I need to calculate bsub as b - x and badd as b + x. However, I don't want underflow/overflow occur during these operations. For example (pseudo-code): b = 3; x = 5; bsub = b - x; // bsub must be 0, not…
ovk
  • 2,318
  • 1
  • 23
  • 30