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
45
votes
8 answers

Java "Bit Shifting" Tutorial?

I would be thankful for a good tutorial, that explain for java newbies how in java all the "bit shifting" work. I always stumble across it, but never understood how it works. It should explain all the operations and concepts that are possible with…
Markus
  • 4,062
  • 4
  • 39
  • 42
45
votes
31 answers

How can I perform multiplication without the '*' operator?

I was just going through some basic stuff as I am learning C. I came upon a question to multiply a number by 7 without using the * operator. Basically it's like this (x << 3) - x; Now I know about basic bit manipulation operations, but I…
Race
  • 625
  • 2
  • 8
  • 9
44
votes
6 answers

What is the purpose of "int mask = ~0;"?

I saw the following line of code here in C. int mask = ~0; I have printed the value of mask in C and C++. It always prints -1. So I do have some questions: Why assigning value ~0 to the mask variable? What is the purpose of ~0? Can we use -1…
Jayesh
  • 4,755
  • 9
  • 32
  • 62
44
votes
11 answers

Speed up bitstring/bit operations in Python?

I wrote a prime number generator using Sieve of Eratosthenes and Python 3.1. The code runs correctly and gracefully at 0.32 seconds on ideone.com to generate prime numbers up to 1,000,000. # from bitstring import BitString def…
44
votes
11 answers

What is the fastest way to return the positions of all set bits in a 64-bit integer?

I need a fast way to get the position of all one bits in a 64-bit integer. For example, given x = 123703, I'd like to fill an array idx[] = {0, 1, 2, 4, 5, 8, 9, 13, 14, 15, 16}. We can assume we know the number of bits a priori. This will be…
Andrew
  • 867
  • 7
  • 20
44
votes
12 answers

What's the fastest way to divide an integer by 3?

int x = n / 3; // <-- make this faster // for instance int a = n * 3; // <-- normal integer multiplication int b = (n << 1) + n; // <-- potentially faster multiplication
Greg Dean
  • 29,221
  • 14
  • 67
  • 78
43
votes
15 answers

Compute fast log base 2 ceiling

What is a fast way to compute the (long int) ceiling(log_2(i)), where the input and output are 64-bit integers? Solutions for signed or unsigned integers are acceptable. I suspect the best way will be a bit-twiddling method similar to those found…
kevinlawler
  • 930
  • 1
  • 9
  • 19
42
votes
8 answers

XOR Operation Intuition

I recently came across this question on Leetcode and figured out a solution that I need some clarification with: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a…
User 5842
  • 2,849
  • 7
  • 33
  • 51
42
votes
5 answers

Is there a way to perform a circular bit shift in C#?

I know that the following is true int i = 17; //binary 10001 int j = i << 1; //decimal 34, binary 100010 But, if you shift too far, the bits fall off the end. Where this happens is a matter of the size of integer you are working with. Is there a…
Jason Z
  • 13,122
  • 15
  • 50
  • 62
42
votes
10 answers

When to use Bitwise Operators during webdevelopment?

Although I grasp the concept of Bitwise Operators, I can't say that I have come across many use cases during the webdevelopment process at which I had to resort to using Bitwise Operators. Do you use Bitwise Operators? Why do you use them? What are…
Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
42
votes
8 answers

How Does The Bitwise & (AND) Work In Java?

I was reading through some code examples and came across a & on Oracle's website on their Bitwise and Bit Shift Operators page. In my opinion it didn't do too well of a job explaining the bitwise &. I understand that it does a operation directly to…
Anzwur
  • 603
  • 1
  • 6
  • 10
42
votes
3 answers

Bits counting algorithm (Brian Kernighan) in an integer time complexity

Can someone explains why Brian Kernighan's algorithm takes O(log N) to count set bits (1s) in an integer. A simple implementation of this algorithm is below (in JAVA) int count_set_bits(int n){ int count = 0; while(n != 0){ n &=…
peter
  • 8,333
  • 17
  • 71
  • 94
41
votes
6 answers

bitwise AND in Javascript with a 64 bit integer

I am looking for a way of performing a bitwise AND on a 64 bit integer in JavaScript. JavaScript will cast all of its double values into signed 32-bit integers to do the bitwise operations (details here).
Toby Hede
  • 36,755
  • 28
  • 133
  • 162
41
votes
9 answers

How to compute a 3D Morton number (interleave the bits of 3 ints)

I'm looking for a fast way to compute a 3D Morton number. This site has a magic-number based trick for doing it for 2D Morton numbers, but it doesn't seem obvious how to extend it to 3D. So basically I have 3 10-bit numbers that I want to interleave…
damndirtyape
  • 473
  • 1
  • 5
  • 5
39
votes
4 answers

Is "-1>>5;" unspecified behavior in C?

C11 §6.5.7 Paragraph 5: The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2*^E2. If…
msc
  • 33,420
  • 29
  • 119
  • 214