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
60
votes
7 answers

BitSet to and from integer/long

If I have an integer that I'd like to perform bit manipulation on, how can I load it into a java.util.BitSet? How can I convert it back to an int or long? I'm not so concerned about the size of the BitSet -- it will always be 32 or 64 bits long. …
ataylor
  • 64,891
  • 24
  • 161
  • 189
60
votes
7 answers

What does AND 0xFF do?

In the following code: short = ((byte2 << 8) | (byte1 & 0xFF)) What is the purpose of & 0xFF? Because sometimes, I see the above code written as: short = ((byte2 << 8) | byte1) And that seems to work fine too.
Maestro
  • 9,046
  • 15
  • 83
  • 116
60
votes
10 answers

How does this bitwise operation check for a power of 2?

I'm looking at some code which should be trivial -- but my math is failing me miserably here. Here's a condition that checks if a number if a power of 2 using the following: if((num != 1) && (num & (num - 1))) { /* make num pow of 2 */ } My…
Coocoo4Cocoa
  • 48,756
  • 50
  • 150
  • 175
58
votes
8 answers

Add two integers using only bitwise operators?

In C#, is it possible to perform a sum of two 32-bit integers without using things like if..else, loops etc? That is, can it be done using only the bitwise operations OR (|), AND (&), XOR (^), NOT (!), shift left (<<) and shift right (>>)?
Delta
  • 4,308
  • 2
  • 29
  • 37
57
votes
13 answers

In Java, how to get positions of ones in reversed binary form of an integer?

I have a legacy application that takes an integer, converts it to a binary string, reverses that string, and then gets the positions of bits (ones) as a list of integers. For example: 6 -> "110" -> "011" -> (2,3) 7 -> "111" -> "111" -> (1,2,3) 8…
workerjoe
  • 2,421
  • 1
  • 26
  • 49
57
votes
3 answers

The difference between logical shift right, arithmetic shift right, and rotate right

I've been reading the classic Hacker's delight and I am having trouble understanding the difference between logical shift right,arithmetic shift right, and rotate right. Please excuse if the doubt seems too simple.
Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
56
votes
10 answers

Can XOR of two integers go out of bounds?

I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The…
Expert Novice
  • 1,943
  • 4
  • 22
  • 47
55
votes
3 answers

Bitwise operations on 32-bit unsigned ints?

JavaScript converts operands to 32-bit signed ints before doing bitwise operations. It also does the operation with 32-bit signed ints, meaning that the result is a 32-bit signed int. Because I'd like to do bitwise operations with 32-bit unsigned…
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
55
votes
17 answers

Algorithm for finding the smallest power of two that's greater or equal to a given value

I need to find the smallest power of two that's greater or equal to a given value. So far, I have this: int value = 3221; // 3221 is just an example, could be any number int result = 1; while (result < value) result <<= 1; It works fine, but feels…
Boyan
  • 3,645
  • 3
  • 22
  • 14
55
votes
5 answers

How to define and work with an array of bits in C?

I want to create a very large array on which I write '0's and '1's. I'm trying to simulate a physical process called random sequential adsorption, where units of length 2, dimers, are deposited onto an n-dimensional lattice at a random location,…
Eddy
  • 6,661
  • 21
  • 58
  • 71
55
votes
8 answers

What is 1 << 0?

enum { kFlag_FPS = 1 << 0, kFlag_Help = 1 << 1, kFlag_RedBlue3D = 1 << 2, } I am trying to understand what this code is I don't quite know what: 1 << 0 means? Any help is greatly appreciated!
CodeDoctorJL
  • 1,216
  • 4
  • 16
  • 29
54
votes
6 answers

Two elements in array whose xor is maximum

Given an array of integers ,You have to find two elements whose XOR is maximum. There is naive approach --just by picking each element and xoring with other elements and then comparing the results to find the pair. Other than this ,Is there any…
Anil Arya
  • 3,100
  • 7
  • 43
  • 69
54
votes
22 answers

How to add two numbers without using ++ or + or another arithmetic operator

How do I add two numbers without using ++ or + or any other arithmetic operator? It was a question asked a long time ago in some campus interview. Anyway, today someone asked a question regarding some bit-manipulations, and in answers a beautiful…
Vivek Sharma
  • 3,794
  • 7
  • 38
  • 48
53
votes
4 answers

Absolute value abs(x) using bitwise operators and Boolean logic

How does this work? The idea is to make abs(x) use bitwise operators for integers (assuming 32 bit words): y = x >> 31 (x + y) ^ y // This gives abs(x) (is ^ XOR)?
53
votes
16 answers

Division without using '/'

Can anyone tell me an efficient approach to perform the division operation without using '/'. I can calculate the integer value in log(n) steps using a method similar to binary search. 115/3 57 * 3 > 115 28 * 3 < 115 47 * 3 > 115 . . . 38 * 3 is…
broun
  • 2,483
  • 5
  • 40
  • 55