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
3 answers

Macros to set and clear bits

Im trying to write a few simple macros to simplify the task of setting and clearing bits which should be a simple task however I cant seem to get them to work correctly. #define SET_BIT(p,n) ((p) |= (1 << (n))) #define CLR_BIT(p,n) ((p) &= (~(1) <<…
volting
  • 16,773
  • 7
  • 36
  • 54
4
votes
2 answers

C Population Count of unsigned 64-bit integer with a maximum value of 15

I use a population count (hamming weight) function intensively in a windows c application and have to optimize it as much as possible in order to boost performance. More than half the cases where I use the function I only need to know the value to a…
4
votes
11 answers

Count bits used in int

If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed below: 101 should return 3 000000011 should return 2 11100 should return 5 101010101 should…
sigvardsen
  • 1,531
  • 3
  • 26
  • 44
4
votes
2 answers

User role permissions for different modules using bitwise operators

So I have an application that has several modules (think of modules as different pages), each module has a set of permissions; view, add, edit, delete I want each user role to have privileges for each module, for example Role A Permissions Module 1…
Serge
  • 1,066
  • 2
  • 7
  • 24
4
votes
2 answers

Bitwise operation on a floating point usefulness

I noticed a SSE instruction existed for floating point AND which got me wondering. You can do the same thing with scalars in fp/integer union. The idea struck me that, if you bitwise OR the components of an array of floats, you can determine if any…
Thomas
  • 6,032
  • 6
  • 41
  • 79
4
votes
2 answers

Shifting digits in C

Is it possible to shift right the digits of an integer? I mean shifting the actual digits of the integer values and not the bits of its binary representation - for example shifting right the number 435 twice would result in 4. I want to know if…
jon Prime
  • 51
  • 1
  • 5
4
votes
2 answers

Bitwise operations to produce power of two in Python

In just intonation theory, a type of music theory, intervals between notes are expressed with rational numbers. Doubling a frequency with a 2:1 ratio brings it up an octave and a 1:1 ratio is no change; a unison. So if I have an interval n that is…
Dan D
  • 153
  • 1
  • 7
4
votes
3 answers

Java Integer.highestOneBit in C#

I have been trying a lot to find an exact replacement for the Java's Integer.highestOneBit(int) in C#. I even tried finding its source code but to no avail. JavaDocs tells that this function: Returns an int value with at most a single one-bit, in…
Nathan
  • 1,303
  • 12
  • 26
4
votes
5 answers

C# .NET: if ((e.State & ListViewItemStates.Selected) != 0) <- What does it mean?

In standard MSN code, there's a line on a ListView - Ownerdraw - DrawItem : if ((e.State & ListViewItemStates.Selected) != 0) { //Draw the selected background } Apparently it does a bitwise comparison for the state ?? Why bitwise ? The…
Run CMD
  • 2,937
  • 3
  • 36
  • 61
4
votes
5 answers

Bit manipulation, permutate bits

I am trying to make a loop that loops through all different integers where exactly 10 of the last 40 bits are set high, the rest set low. The reason is that I have a map with 40 different values, and I want to sum all different ways ten of these…
triktae
  • 123
  • 4
4
votes
2 answers

Faster way to perform bit shuffling on an integer with Java

I am wondering if there a faster way for shuffling bits of an integer rather than the following public int shuffleBits(int number) { int int_width = 31; Random random = new Random(); for(int i = 0; i < int_width; i++) { number =…
peter
  • 8,333
  • 17
  • 71
  • 94
4
votes
4 answers

quickly rounding numbers >= 0 up to the multiple of a specific power of 2

There's a widely known pattern for rounding numbers up to the nearest multiple of a power of two. Increment the number by one less than the power of two, and then wipe out any bits below it: power = 1 << i (n + (power - 1)) & ~(power - 1) The…
strcat
  • 5,376
  • 1
  • 27
  • 31
4
votes
1 answer

Unexpected result in PHP bitwise operation

The operation 1539 | 0xfffff800 returns -509 in JavaScript and Python 2.7. In PHP I get 4294966787. Does anybody know why and could explain that to me. I would love to know how I get the expected result in PHP as well.
gsirin
  • 41
  • 1
4
votes
3 answers

Rotate right using bit operation in c

I am trying to come up with a function int rotateRight (int x, int n) that rotates x to the right by n. For example, rotateRight(0x87654321,4) = 0x76543218 This is what I have so far: int rotateRight(int x, int n) { int mask = (((1 <<…
paupau
  • 57
  • 1
  • 2
  • 9
4
votes
3 answers

Determine if an Enum with FlagsAttribute has unique bit values

Consider the following enumeration: [Flags] public enum EnumWithUniqueBitFlags { None = 0, One = 1, Two = 2, Four = 4, Eight = 8, } [Flags] public enum EnumWithoutUniqueFlags { None = 0, One = 1, Two = 2, Four =…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
1 2 3
99
100