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
39
votes
4 answers

Bit count : preprocessor magic vs modern C++

Suppose that I want to create a compile time constructed bit count lookup table for 64bit integers in 16 bit chunks. The only way I know to do this is the following code: #define B4(n) n, n + 1, n + 1, n + 2 #define B6(n) B4(n), B4(n + 1), …
fitzbutz
  • 956
  • 15
  • 33
39
votes
3 answers

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

How do I set, clear and toggle a bit in Rust?
zzeroo
  • 5,466
  • 4
  • 33
  • 49
39
votes
6 answers

Do bitwise operators (other than shifts) make any mathematical sense in base-10?

According to wiki shifts can be used to calculate powers of 2: A left arithmetic shift by n is equivalent to multiplying by 2^n (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is…
serg
  • 109,619
  • 77
  • 317
  • 330
39
votes
9 answers

How to find the position of the only-set-bit in a 64-bit value using bit manipulation efficiently?

Just say I have a value of type uint64_t seen as sequence of octets (1 octet = 8-bit). The uint64_t value is known containing only one set bit at a MSB position. Thus, the uint64_t value can be in one of the following binary…
Aeoliyan
  • 835
  • 8
  • 13
39
votes
3 answers

Fastest way to get sign in Java?

I'd like to get the sign of a float value as an int value of -1 or 1. Avoiding conditionals is always a good idea in reducing computational cost. For instance, one way I can think of would be to use a fast bit-shift to get the sign: float a =…
Engineer
  • 8,529
  • 7
  • 65
  • 105
38
votes
6 answers

Change a bit of an integer

We have an integer number int x = 50; in binary, it's 00110010 How can I change the fourth (4th) bit programatically?
pedram
  • 381
  • 1
  • 3
  • 4
38
votes
16 answers

Fast divisibility tests (by 2,3,4,5,.., 16)?

What are the fastest divisibility tests? Say, given a little-endian architecture and a 32-bit signed integer: how to calculate very fast that a number is divisible by 2,3,4,5,... up to 16? WARNING: given code is EXAMPLE only. Every line is…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
38
votes
2 answers

bitwise & doesn't work with bytes in kotlin

I'm trying to write kotlin code like: for (byte b : hash) stringBuilder.append(String.format("%02x", b&0xff)); but I have nothing to do with the "&". I'm trying to use "b and 0xff" but it doesn't work. The bitwise "and" seems to work on Int,…
Allen Vork
  • 1,536
  • 3
  • 16
  • 29
38
votes
3 answers

Java storing two ints in a long

I want to store two ints in a long (instead of having to create a new Point object every time). Currently, I tried this. It's not working, but I don't know what is wrong with it: // x and y are ints long l = x; l = (l << 32) | y; And I'm getting…
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
37
votes
5 answers

What is the efficient way to count set bits at a position or lower?

Given std::bitset<64> bits with any number of bits set and a bit position X (0-63) What is the most efficient way to count bits at position X or lower or return 0 if the bit at X is not set Note: If the bit is set the return will always be at least…
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
37
votes
12 answers

How to get the Nth digit of an integer with bit-wise operations?

Example. 123456, and we want the third from the right ('4') out. The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1). C/C++/C# preferred.
John Smith
  • 631
  • 1
  • 6
  • 7
37
votes
1 answer

What is an XOR sum?

I am not sure of the precise definition of this term. I know that a bitwise XOR operation is going bit by bit and taking the XOR of corresponding bits position wise. Is this result termed the 'XOR sum'? If not, what is an XOR sum, and how do you…
user2517777
  • 741
  • 2
  • 7
  • 9
36
votes
3 answers

Should I bit-shift to divide by 2 in Java?

Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick Java Optimization Question Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is…
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
36
votes
6 answers

Flags enum & bitwise operations vs. “string of bits”

A fellow developer suggested we store a selection of days of the week as 7-character string of 1’s and 0’s, i.e. “1000100” for Monday and Friday. I preferred (and strongly suggested) a solution with a Flags enum and bitwise operations, I think it's…
Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
35
votes
8 answers

n is negative, positive or zero? return 1, 2, or 4

I'm building a PowerPC interpreter, and it works quite well. In the Power architecture the condition register CR0 (EFLAGS on x86) is updated on almost any instruction. It is set like this. The value of CR0 is 1, if the last result was negative, 2 if…
Francesco Boffa
  • 1,402
  • 1
  • 19
  • 35