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
3109
votes
27 answers

How to set, clear, and toggle a single bit

How can I set, clear, and toggle a bit?
JeffV
  • 52,985
  • 32
  • 103
  • 124
1539
votes
10 answers

What are bitwise shift (bit-shift) operators and how do they work?

I've been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators)... At a core level, what does bit-shifting (<<, >>, >>>) do, what problems can it help solve, and what…
1007
votes
65 answers

Count the number of set bits in a 32-bit integer

8 bits representing the number 7 look like this: 00000111 Three bits are set. What are the algorithms to determine the number of set bits in a 32-bit integer?
Matt Howells
  • 40,310
  • 20
  • 83
  • 102
701
votes
48 answers

Divide a number by 3 without using *, /, +, -, % operators

How would you divide a number by 3 without using *, /, +, -, %, operators? The number may be signed or unsigned.
Green goblin
  • 9,898
  • 13
  • 71
  • 100
504
votes
24 answers

What is “two's complement”?

I'm in a computer systems course and have been struggling, in part, with two's complement. I want to understand it, but everything I've read hasn't brought the picture together for me. I've read the Wikipedia article and various other articles,…
326
votes
5 answers

Extracting bits with a single multiplication

I saw an interesting technique used in an answer to another question, and would like to understand it a little better. We're given an unsigned 64-bit integer, and we are interested in the following…
NPE
  • 486,780
  • 108
  • 951
  • 1,012
282
votes
6 answers

What is bit masking?

I am fairly new to C programming, and I encountered bit masking. What is the general concept and function of bit masking? Examples are much appreciated.
Mr.Z
  • 2,929
  • 3
  • 14
  • 6
281
votes
31 answers

Rounding up to next power of 2

I want to write a function that returns the nearest next power of 2 number. For example if my input is 789, the output should be 1024. Is there any way of achieving this without using any loops but just using some bitwise operators? Related:…
Naveen
  • 74,600
  • 47
  • 176
  • 233
266
votes
27 answers

Efficient Algorithm for Bit Reversal (from MSB->LSB to LSB->MSB) in C

What is the most efficient algorithm to achieve the following: 0010 0000 => 0000 0100 The conversion is from MSB->LSB to LSB->MSB. All bits must be reversed; that is, this is not endianness-swapping.
green_t
  • 2,997
  • 3
  • 21
  • 18
235
votes
7 answers

Using bitwise OR 0 to floor a number

A colleague of mine stumbled upon a method to floor float numbers using a bitwise or: var a = 13.6 | 0; //a == 13 We were talking about it and wondering a few things. How does it work? Our theory was that using such an operator casts the number to…
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
233
votes
5 answers

What does a tilde do when it precedes an expression?

var attr = ~'input,textarea'.indexOf( target.tagName.toLowerCase() ) ? 'value' : 'innerHTML' I saw it in an answer, and I've never seen it before. What does it mean?
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
218
votes
11 answers

Most common C# bitwise operations on enums

For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to have. For example: flags = flags | FlagsEnum.Bit4; …
steffenj
  • 7,967
  • 10
  • 35
  • 41
212
votes
9 answers

C# int to byte[]

I need to convert an int to a byte[] one way of doing it is to use BitConverter.GetBytes(). But im unsure if that matches the following specification: An XDR signed integer is a 32-bit datum that encodes an integer in the range…
Peter
  • 37,042
  • 39
  • 142
  • 198
200
votes
12 answers

Explain the use of a bit vector for determining if all characters are unique

I am confused about how a bit vector would work to do this (not too familiar with bit vectors). Here is the code given. Could someone please walk me through this? public static boolean isUniqueChars(String str) { int checker = 0; for (int i…
user1136342
  • 4,731
  • 10
  • 30
  • 40
194
votes
1 answer

What is the meaning of double tilde (~~) in Java?

When browsing the source code of Guava, I came across the following piece of code (part of the implementation of hashCode for the inner class CartesianSet): int adjust = size() - 1; for (int i = 0; i < axes.size(); i++) { adjust *= 31; …
bisgardo
  • 4,130
  • 4
  • 29
  • 38
1
2 3
99 100