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
86
votes
5 answers

Set specific bit in byte

I'm trying to set bits in Java byte variable. It does provide propper methods like .setBit(i). Does anybody know how I can realize this? I can iterate bit-wise through a given byte: if( (my_byte & (1 << i)) == 0 ){ } However I cannot set this…
wishi
  • 7,188
  • 17
  • 64
  • 103
85
votes
12 answers

How does XOR variable swapping work?

Can someone explain to me how XOR swapping of two variables with no temp variable works? void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } I understand WHAT it does, but can someone walk…
mmcdole
  • 91,488
  • 60
  • 186
  • 222
84
votes
7 answers

Why if (n & -n) == n then n is a power of 2?

Line 294 of java.util.Random source says if ((n & -n) == n) // i.e., n is a power of 2 // rest of the code Why is this?
David Weng
  • 4,165
  • 12
  • 42
  • 51
84
votes
7 answers

Efficiently find binary strings with low Hamming distance in large set

Problem: Given a large (~100 million) list of unsigned 32-bit integers, an unsigned 32-bit integer input value, and a maximum Hamming Distance, return all list members that are within the specified Hamming Distance of the input value. Actual data…
Eric J.
  • 147,927
  • 63
  • 340
  • 553
84
votes
5 answers

Bitwise operators and "endianness"

Does endianness matter at all with the bitwise operations? Either logical or shifting? I'm working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I'm getting quite hung up on the endianess. That…
Frank V
  • 25,141
  • 34
  • 106
  • 144
83
votes
42 answers

Have you ever had to use bit shifting in real projects?

Have you ever had to use bit shifting in real programming projects? Most (if not all) high level languages have shift operators in them, but when would you actually need to use them?
Philip Morton
  • 129,733
  • 38
  • 88
  • 97
81
votes
19 answers

Explain this snippet which finds the maximum of two integers without using if-else or any other comparison operator?

Find the maximum of two numbers. You should not use if-else or any other comparison operator. I found this question on online bulletin board, so i thought i should ask in StackOverflow EXAMPLE Input: 5, 10 Output: 10 I found this solution, can…
SuperMan
  • 3,532
  • 12
  • 45
  • 49
81
votes
3 answers

Incrementing 'masked' bitsets

I'm currently in the process of writing a tree enumerator where I've come across the following problem: I'm looking at masked bitsets, i.e. bitsets where the set bits are a subset of a mask, i.e. 0000101 with mask 1010101. What I want to accomplish…
Tobias Ribizel
  • 5,331
  • 1
  • 18
  • 33
80
votes
8 answers

Subtracting packed 8-bit integers in an 64-bit integer by 1 in parallel, SWAR without hardware SIMD

If I have a 64-bit integer that I'm interpreting as an array of packed 8-bit integers with 8 elements. I need to subtract the constant 1 from each packed integer while handling overflow without the result of one element affecting the result of…
cam-white
  • 710
  • 5
  • 9
76
votes
19 answers

Simplest way to check if two integers have same sign?

Which is the simplest way to check if two integers have same sign? Is there any short bitwise trick to do this?
Gerber
75
votes
16 answers

Given an integer, how do I find the next largest power of two using bit-twiddling?

If I have a integer number n, how can I find the next number k > n such that k = 2^i, with some i element of N by bitwise shifting or logic. Example: If I have n = 123, how can I find k = 128, which is a power of two, and not 124 which is only…
AndreasT
  • 9,417
  • 11
  • 46
  • 60
74
votes
8 answers

What does bitwise XOR (exclusive OR) mean?

I'm trying to understand the binary operators in C# or in general, in particular ^ - exclusive or. For example: Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
73
votes
12 answers

Checking if a bit is set or not

How to check if a certain bit in a byte is set? bool IsBitSet(Byte b,byte nPos) { return .....; }
Manjoor
  • 4,091
  • 10
  • 43
  • 67
72
votes
6 answers

Arithmetic bit-shift on a signed integer

I am trying to figure out how exactly arithmetic bit-shift operators work in C, and how it will affect signed 32-bit integers. To make things simple, let's say we work within one byte (8 bits): x = 1101.0101 MSB[ 1101.0101 ]LSB Reading other posts…
newprint
  • 6,936
  • 13
  • 67
  • 109
72
votes
5 answers

What is (x & 1) and (x >>= 1)?

I am trying to do assignment: "Find the number of bits in an unsigned integer data type without using the sizeof() function." And my design is to convert the integer to bits and then to count them. For ex: 10 is 1010 and 5 is 101 Converting integer…
Sandra K
  • 1,209
  • 1
  • 10
  • 21