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

Send and receive binary data over web sockets in Javascript?

It is possible to send and receive binary data over web sockets in Javascript? Could I, for example, implement an SSH client using web sockets?
Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
35
votes
15 answers

Bit twiddling: which bit is set?

I have a 64-bit unsigned integer with exactly 1 bit set. I’d like to assign a value to each of the possible 64 values (in this case, the odd primes, so 0x1 corresponds to 3, 0x2 corresponds to 5, …, 0x8000000000000000 corresponds to 313). It seems…
Charles
  • 11,269
  • 13
  • 67
  • 105
35
votes
5 answers

Why does ((unsigned char)0x80) << 24 get sign extended to 0xFFFFFFFF80000000 (64-bit)?

The following program #include /* printf(" %" PRIu32 "\n"), my_uint32_t) */ #include /* printf(), perror() */ int main(int argc, char *argv[]) { uint64_t u64 = ((unsigned char)0x80) << 24; printf("%" PRIX64 "\n",…
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
35
votes
4 answers

Set a specific bit in an int

I need to mask certain string values read from a database by setting a specific bit in an int value for each possible database value. For example, if the database returns the string "value1" then the bit in position 0 will need to be set to 1, but…
Michael Kingsmill
  • 1,815
  • 3
  • 22
  • 31
35
votes
6 answers

Need help understanding "getbits()" method in Chapter 2 of K&R C

In chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works. Here's the method provided: unsigned int getbits(unsigned int x, int p, int n) { return (x >> (p + 1 - n)) & ~(~0…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
35
votes
15 answers

Why is squaring a number faster than multiplying two random numbers?

Multiplying two binary numbers takes n^2 time, yet squaring a number can be done more efficiently somehow. (with n being the number of bits) How could that be? Or is it not possible? This is insanity!
Jess
  • 3,111
  • 3
  • 22
  • 17
35
votes
11 answers

How to compute the integer absolute value

How to compute the integer absolute value without using if condition. I guess we need to use some bitwise operation. Can anybody help?
Pragyan
  • 353
  • 1
  • 3
  • 5
34
votes
14 answers

Find out number of bits needed to represent a positive integer in binary?

This is probably pretty basic, but to save me an hour or so of grief can anyone tell me how you can work out the number of bits required to represent a given positive integer in Java? e.g. I get a decimal 11, (1011). I need to get the answer, 4. I…
joinJpegs
  • 1,287
  • 3
  • 14
  • 21
34
votes
6 answers

What is the purpose of the unsigned right shift operator ">>>" in Java?

I understand what the unsigned right shift operator ">>>" in Java does, but why do we need it, and why do we not need a corresponding unsigned left shift operator?
Tyler Durden
  • 11,156
  • 9
  • 64
  • 126
34
votes
8 answers

Is & faster than % when checking for odd numbers?

To check for odd and even integer, is the lowest bit checking more efficient than using the modulo? >>> def isodd(num): return num & 1 and True or False >>> isodd(10) False >>> isodd(9) True
riza
  • 16,274
  • 7
  • 29
  • 29
33
votes
4 answers

Is there a faster algorithm for max(ctz(x), ctz(y))?

For min(ctz(x), ctz(y)), we can use ctz(x | y) to gain better performance. But what about max(ctz(x), ctz(y))? ctz represents "count trailing zeros". C++ version (Compiler Explorer) #include #include #include int32_t…
QuarticCat
  • 1,314
  • 6
  • 20
33
votes
9 answers

How to convert an int to a little endian byte array?

I have this function in C# to convert a little endian byte array to an integer number: int LE2INT(byte[] data) { return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; } Now I want to convert it back to little endian.. Something…
Yana D. Nugraha
  • 5,069
  • 10
  • 45
  • 59
33
votes
9 answers

"Isolate" specific Row/Column/Diagonal from a 64-bit number

OK, let's consider a 64-bit number, with its bits forming a 8x8 table. E.g. 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0 written as a b c d e f g…
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
32
votes
18 answers

Checking whether a number is positive or negative using bitwise operators

I can check whether a number is odd/even using bitwise operators. Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc. Can the same be done using bitwise operators and some…
Anirudhh Er
  • 321
  • 1
  • 3
  • 3
32
votes
2 answers

Is unsetting a single bit in flags safe with Python variable-length integers?

In my program (written in Python 3.4) I have a variable which contains various flags, so for example: FLAG_ONE = 0b1 FLAG_TWO = 0b10 FLAG_THREE = 0b100 status = FLAG_ONE | FLAG_TWO | FLAG_THREE Setting another flag can easily be done with status |=…
elzell
  • 2,228
  • 1
  • 16
  • 26