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
32
votes
10 answers

Getting the fractional part of a float without using modf()

I'm developing for a platform without a math library, so I need to build my own tools. My current way of getting the fraction is to convert the float to fixed point (multiply with (float)0xFFFF, cast to int), get only the lower part (mask with…
knight666
  • 1,599
  • 3
  • 22
  • 38
32
votes
6 answers

Convert Byte Array to Bit Array?

How would I go about converting a bytearray to a bit array?
cam
  • 8,725
  • 18
  • 57
  • 81
32
votes
5 answers

How to set/unset a bit at specific position of a long?

How to set/unset a bit at specific position of a long in Java ? For example, long l = 0b001100L ; // bit representation I want to set bit at position 2 and unset bit at position 3 thus corresponding long will be, long l = 0b001010L ; // bit…
Arpssss
  • 3,850
  • 6
  • 36
  • 80
31
votes
8 answers

Using Bitwise operators on flags

I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All = 0x7 Say I receive the two flags Past and Future (setFlags(PAST | FUTURE)). How can I tell if Past is in it? Likewise how can I tell that Current is not in it? That way I don't…
Malfist
  • 31,179
  • 61
  • 182
  • 269
31
votes
12 answers

Efficiently count the number of bits in an integer in JavaScript

Let's say I have an integer I and want to get the count of 1s in its binary form. I am currently using the following code. Number(i.toString(2).split("").sort().join("")).toString().length; Is there a faster way to do this? I am thinking about…
Ming Huang
  • 1,310
  • 3
  • 16
  • 25
31
votes
2 answers

Bitshift and integer promotion?

Normally, C requires that a binary operator's operands are promoted to the type of the higher-ranking operand. This can be exploited to avoid filling code with verbose casts, for example: if (x-48U<10) ... y = x+0ULL << 40; etc. However, I've found…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
31
votes
2 answers

why is 1>>32 == 1?

I'm wondering if perhaps this is a JVM bug? java version "1.6.0_0" OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu13) OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode) class Tmp { public static void main(String[] args) { …
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
31
votes
6 answers

Zig Zag Decoding

In the google protocol buffers encoding overview, they introduce something called "Zig Zag Encoding", this takes signed numbers, which have a small magnitude, and creates a series of unsigned numbers which have a small magnitude. For example Encoded…
31
votes
3 answers

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned, which have suffixes l and ll): — Built-in…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
31
votes
3 answers

Why in Java (high + low) / 2 is wrong but (high + low) >>> 1 is not?

I understand the >>> fixes the overflow: when adding two big positive longs you may end up with a negative number. Can someone explain how this bitwise shift magically fixes the overflow problem? And how it is different from >> ? My suspicion: I…
JohnPristine
  • 3,485
  • 5
  • 30
  • 49
31
votes
7 answers

Bits list to integer in Python

I have such list in Python: [1,0,0,0,0,0,0,0]. Can I convert it to integer like as I've typed 0b10000000 (i.e. convert to 128)? I need also to convert sequences like [1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0] to integers (here it will return…
ghostmansd
  • 3,285
  • 5
  • 30
  • 44
30
votes
8 answers

How to create a byte out of 8 bool values (and vice versa)?

I have 8 bool variables, and I want to "merge" them into a byte. Is there an easy/preferred method to do this? How about the other way around, decoding a byte into 8 separate boolean values? I come in assuming it's not an unreasonable question, but…
xcel
  • 377
  • 1
  • 3
  • 6
30
votes
4 answers

De Bruijn-like sequence for `2^n - 1`: how is it constructed?

I'm looking at the entry Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup from Bit Twiddling hacks. I can easily see how the second algorithm in that entry works static const int…
30
votes
8 answers

f(int x) { return x == 0 ? 0 : 1; } in Java without conditionals

I want to implement f(int x) { return x == 0 ? 0 : 1; } in Java. In C, I'd just "return !!x;", but ! doesn't work like that in Java. Is there some way to do it without conditionals? Without something cheesy like an unrolled version of int ret =…
CromTheDestroyer
  • 3,616
  • 3
  • 20
  • 26
30
votes
10 answers

How can I perform multiplication, using bitwise operators?

I am working through a problem which I was able to solve, all but for the last piece—I am not sure how one can do multiplication using bitwise operators: 0*8 = 0 1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 Is there an approach to solve this?
James Raitsev
  • 92,517
  • 154
  • 335
  • 470