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
4
votes
9 answers

Find first unset bit in buffer (optimization)

What's the fastest/cleanest way to find the bit offset of the first unset bit in an array of arbitrary length? Assume the prototype for your function is something like this size_t first_unset_bit(char unsigned const *buf, size_t bit_count, size_t…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
4
votes
2 answers

Setting/clearing bits: bitshift or bitmask lookup?

I'm working on a bitboard-based chess engine, and one of the actions performed profusely is setting/clear bits in an unsigned 64-bit integer. Since I'm not that well-versed in what code will run 'faster' on certain processors, this is something I…
Shreyas
  • 667
  • 2
  • 7
  • 20
4
votes
2 answers

Decrementing a value and wrapping back to a max value without using if statement?

Incrementing and wrapping back to zero is easy: i = (i + 1) % Max; But decrementing is not as straight forward. Basically to decrement and wrap you usually find code like this: i--; if (i < 0) i = Max; How can we write the previous code without…
vexe
  • 5,433
  • 12
  • 52
  • 81
4
votes
5 answers

Efficient bitswapping in python3

I have a very hard time handling bitswapping in python3. So far I found fast bitswapping algorithms in C here and here, but I wasn't able to properly translate that into python3, because handling the data, using the correct data types and not…
jmm
  • 384
  • 2
  • 12
4
votes
1 answer

Find first bit, find last bit assembly using bsf, blf

I'm looking at this code: http://lxr.free-electrons.com/source/arch/x86/include/asm/bitops.h static inline unsigned long __ffs(unsigned long word) { asm("rep; bsf %1,%0" : "=r" (word) : "rm" (word)); …
w00d
  • 5,416
  • 12
  • 53
  • 85
4
votes
6 answers

Looping through Bits C

I'm trying to loop through the bits of an unsigned char, but I'm not sure where to start, eventually, I'll perform other bitwise operating on the bits, such as ~ and xor..etc.
WTL
  • 103
  • 1
  • 3
  • 9
4
votes
3 answers

Shifting BigInteger of Java by long variable

I know there are methods shiftLeft(int n) and shiftRight(int n) for BigInteger class which only takes int type as an argument but I have to shift it by a long variable. Is there any method to do it?
Mani Sankar
  • 800
  • 1
  • 9
  • 19
4
votes
2 answers

Well-defined bit-shifting more than 32 bits w/o compiler warning

This is a silly question partly for fun. I have a "well-defined" (or "saturated"?) bit-mask function template uint32_t mask(uint32_t x) { const uint32_t MASK = N >= 32 ? ~uint32_t(0) : (uint32_t(1) << N) - 1; return x &…
nodakai
  • 7,773
  • 3
  • 30
  • 60
4
votes
5 answers

bitwise shifiting question

if i have int temp=(1<<31)>>31. How come the temp becomes -1? how do i get around this problem? thanks
w31
  • 265
  • 1
  • 2
  • 5
4
votes
4 answers

Is using enum for integer bit oriented operations in C++ reliable/safe?

Consider the following (simplified) code: enum eTestMode { TM_BASIC = 1, // 1 << 0 TM_ADV_1 = 1 << 1, TM_ADV_2 = 1 << 2 }; ... int m_iTestMode; // a "bit field" bool isSet( eTestMode tsm ) { return (…
PeterK
  • 6,287
  • 5
  • 50
  • 86
4
votes
6 answers

Set individual bit in C++

I have a 5 byte data element and I need some help in figuring out how in C++ to set an individual bit of one of these byte; Please see my sample code below: char m_TxBuf[4]; I would like to set bit 2 to high of byte m_TxBuf[1]. 00000 0 00 …
JB_SO
  • 471
  • 3
  • 8
  • 18
4
votes
6 answers

Number of zero bits in integer except leading zeros

If I have an integer in Java how do I count how many bits are zero except for leading zeros? We know that integers in Java have 32 bits but counting the number of set bits in the number and then subtracting from 32 does not give me what I want…
user466534
4
votes
3 answers

Create double by appending binary representations of two ints

I'm trying to create a double by appending the binary representations of two ints. This is what I have now: int i = arc4random(); *(&i+1) = arc4random(); double d = *(double*)&i; I hoped the double pointer would also use the value in the &i+1…
Rugen Heidbuchel
  • 377
  • 2
  • 14
4
votes
4 answers

Bit shifting and bit mask - sample code

I've come across some code which has the bit masks 0xff and 0xff00 or in 16 bit binary form 00000000 11111111 and 11111111 00000000. /** * Function to check if the given string is in GZIP Format. * * @param inString String to check. * @return…
PDStat
  • 5,513
  • 10
  • 51
  • 86
4
votes
1 answer

Unpack redis set bit string in Go

Using redis#Setbit to set bit in a key like: redis.Do("SETBIT", "mykey", 1, 1). When I read it using redis#Get like redis.Do("GET", "mykey"), I get a bit string. How do I unpack the string so I can get a slice of bools in Go? In Ruby, you use…
sent-hil
  • 18,635
  • 16
  • 56
  • 74
1 2 3
99
100