bit-twiddling

Suggestions on how to make a configurable parser.

I want to build a parser for a C like language. The interesting aspect about it is that I want to build it in such a way that someone who has access to the source can easily modified it to extend the language (a new expression type of instance) with the extensions being runtime configurable (they can be turned on and off). My current in...

How should I do an equality test for 80bit IEEE floating point?

related to: comparing ieee floats and doubles for equality Should we compare floating point numbers for equality against a relative error Most effective way for float and double comparison However with regard to 80 bit IEEE floats (see section 8.2) on an x86 In particular I like the this implementation using a count of representable...

Detecting single one-bit streams within an integer

I have to check a number if it satisfies the following criteria: in binary, all one-bits must be successive. the number must have at least one bit set. the successive one-bits may start at the MSB or end at the LSB, so it's perfectly valid if the number is made up of a single one-bit stream followed by a zero-bit stream or vice versa....

What is the best way to learn about twiddling with binary data?

What is the best way to learn about twiddling with binary data? Mainly what I'm referring to here is learning to reading/writing file existing file formats that are in binary. I saw one of my co-workers today trying to learn how to create a flash file from scratch, and he was writing a whole bunch of ones and zeros, but I'm guessing ...

Position of least significant bit that is set

I am looking for an efficient way to determine the position of the least significant bit that is set in an integer, e.g. for 0x0FF0 it would be 4. A trivial implementation is this: unsigned GetLowestBitPos(unsigned value) { assert(value != 0); // handled separately unsigned pos = 0; while (!(value & 1)) { value >>= ...

How can I set all bits to '1' in a binary number of an unknown size?

I'm trying to write a function in assembly (but lets assume language agnostic for the question). How can I use bitwise operators to set all bits of a passed in number to 1? I know that I can use the bitwise "or" with a mask with the bits I wish to set, but I don't know how to construct a mask based off some a binary number of N size....

Find a pattern of binary numbers using shift-right and bitwise-AND?

I'm attempting to write a function in assembly that will detect if a longer binary number contains a smaller binary pattern. Example: Does 100111 contain 1001? When I read this problem I figured that I would do a bitwise-AND with the large number and its smaller pattern while shifting right (logical) each time in a loop. So, in my hea...

toggle two bits with a single operation in C?

Lets say I have a byte with 6 unknown values: ???1?0?? and I want to swap bits 2 and 4 (without changing any of the ? values): ???0?1?? But how would I do this in one operation in C? I'm performing this operation thousands of times per second on a microcontroller so performance is the top priority. EDIT It would be fine to "toggle...

C - Need to compare `n` lowest bits of an int for equality.

C - Need to compare n lowest bits of an int for equality. I.e. n = 4; xxxx1001 == xxxx1001 (x is don't care) I.e. n = 2; xxxxxx01 == xxxxxx01 Can't think of a nice way to do it without using masks, =). ...

How might I set the bottom 3 bytes of a 4-byte long while leaving the top byte intact?

Relevant code is this: typedef unsigned long int chunk_head; typedef struct malloc_chunk { // Contains the size of the data in the chunk and the flag byte. chunk_head head; // Deliberately left unsized to allow overflow. // Contains the payload of the chunk. unsigned int data[]; }; And just as an example...

find string of N 1-bits in a bit-array

As the title sais I want to find a successive run of n one-bits in a bit-array of variable size (M). The usual use-case is N <= 8 and M <= 128 I do this operation a lot in an innerloop on an embedded device. Writing a trivial implementation is easy but not fast enough for my taste (e.g. brute force search until a solution is found). I...

Extracting rightmost N bits of an integer

In the yester Code Jam Qualification round http://code.google.com/codejam/contest/dashboard?c=433101#s=a&amp;a=0 , there was a problem called Snapper Chain. From the contest analysis I came to know the problem requires bit twiddling stuff like extracting the rightmost N bits of an integer and checking if they all are 1. I saw a contestan...

How to calculate 2^n-1 efficiently without overflow?

I want to calculate 2n-1 for a 64bit integer value. What I currently do is this for(i=0; i<n; i++) r|=1<<i; and I wonder if there is more elegant way to do it. The line is in an inner loop, so I need it to be fast. I thought of r=(1ULL<<n)-1; but it doesn't work for n=64, because << is only defined for values of n up to 63. ...

bit twiddling in java - decomposing long into long[] of bitmasks

I am decomposing a single long into a long[] of single bit longs by public static int decompose(long[] buffer, long base) { int count = Long.bitCount(base); for (int i=0; i<count; i++) { base ^= (buffer[i] = Long.lowestOneBit(base)); } return count; } but I feel like there might be a faster way to do this, since it seems l...

bit twiddling in java - power set, ordered by size

Following up my previous bit-twiddling question, now I'm looking to trim down the method that uses that one (though, an unbuffered version, since the life of the array is only this object). This is the iterator method for a power set of some long base; the actual contents of the set isn't stored - it would be a memory hog and individual...