bit-manipulation

bit twiddling: find next power of two

If I have a integer number n. How can I find the next number k > n : k = 2^i, with some i element of N By bitwise shifting or logic. Example: If I have n=123 how can I find k=128 which is a power of two: 2^7(=i) and not 124 which is only divisible by two. It should be simple, but it eludes me. ...

Bit twiddling: find next power of two with templates in c++

This is a follow-up to my general question: bit-twiddling-find-next-power-of-two I have now created the following template function: template <typename T> T nextPowerOfTwo(T n) { std::size_t k=1; n--; do { n |= n >> k ; k <<=1; } while (k < sizeof(T)*8) return ++n; } 2 Questions: Specifying ...

Help me understand why page sizes are a power of 2?

Answer I need help with is: Recall that paging is implemented by breaking up an address into a page and offset number. It is most efficient to break the address into X page bits and Y offset bits, rather than perform arithmetic on the address to calculate the page number and offset. Because each bit position represents...

How would you perform this bitwise operation?

For a particular application using bitwise masks to store a value, I'd like to perform a certain bitwise filter, but I'm not sure what it's called, or how you'd actually do it in a sensible way. // I'll just refer to the operator as "?" for the sake of example: Input1 ? Input2 = Output 0 ? 0 = 0 0 ? 1 = 0 1 ? 0 ...

Language features: optimizing booleans to flags + bitmasks.

I'm writing a programming language, and when I came across this question, my immediate thought was that languages should optimize booleans into bit flags for the programmer. This would keep all the benefits of speed and efficient memory usage while removing the burden of maintenance and the possibilities for errors caused by the more co...

How do I compare two longs as unsigned in Java?

I'm storing bit patterns of unsigned 64-bit numbers in a long variable and want to calculate the distance between two of them on the unsigned range. Because Java interprets long as a two's complement signed integer, I can't just do a - b, as the following example shows: // on the unsigned range, these numbers would be adjacent long a = ...

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, =). ...

visiting all free slots in a bitfield

I have an array of uint64 and for all unset bits (0s), I do some evaluations. The evaluations are not terribly expensive, but very few bits are unset. Profiling says that I spend a lot of time in the finding-the-next-unset-bit logic. Is there a faster way (on a Core2duo)? My current code can skip lots of high 1s: for(int y=0; y<hei...

A C preprocessor macro to pack bitfields into a byte?

I'm getting into microcontroller hacking and while I'm very comfortable with bitwise operators and talking right to the hardware, I'm finding the resulting code very verbose and boilerplate. The higher level programmer in me wants to find an effective but efficient way to clean it up. For instance, there's a lot of setting flags in reg...

How do you set, clear and toggle a single bit in JavaScript?

How to set, clear, toggle and check a bit in JavaScript? ...

(Bitwise) Supersets and Subsets in MySQL

Are the following queries effective in MySQL: SELECT * FROM table WHERE field & number = number; # to find values with superset of number's bits SELECT * FROM table WHERE field | number = number; # to find values with subset of number's bits ...if an index for the field has been created? If not, is there a way to make it run faste...

number of 1's in 32 bit number

I am lookin for a method to have number of 1's in 32 bit number without using a loop in between. can any body help me and provide me the code or algorithm to do so. Thanks in advance. ...

Manipulating Nybbles and smaller in c#

I have an application that requires manipulating nybbles and possibly even individual bits at a time. Is there a library in C# that can help me? ...

Is a bit field any more efficient (computationally) than masking bits and extracting the data by hand?

I have a numerous small pieces of data that I want to be able to shove into one larger data type. Let's say that, hypothetically, this is a date and time. The obvious method is via a bit field like this. struct dt { unsigned long minute :6; unsigned long hour :5; unsigned long day :5; unsigned long month :4; unsigned...

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { unsigned int version : 3; unsigned int type : 1; unsigned int id : 5; unsigned int ...

How do I determine the position of the nth most significand bit set in a 64-bit value?

I'm using some long values as bitmaps in a Java program. Here's my method so far: public class BitmapUtil { private static final long _B_MASK_LEFT_ON = 0x8000000000000000L; public static long setNthMsb(int n) { return BitmapUtil._B_MASK_LEFT_ON >>> n; } public static boolean isNthMsbSet(long b, int n) {...

Counting common bits in a sequence of unsigned longs

I am looking for a faster algorithm than the below for the following. Given a sequence of 64-bit unsigned integers, return a count of the number of times each of the sixty-four bits is set in the sequence. Example: 4608 = 0000000000000000000000000000000000000000000000000001001000000000 4097 = 000000000000000000000000000000000000000000...

What is being done in this bit shifting operation?

(INBuffer[3] << 8) + INBuffer[2] Is this essentially moving the bit in INBuffer[3] into INBuffer[2] or [3] being zero'ed out then added to [2]? ...

Best way to merge hex strings in c++? [heavily edited]

I have two hex strings, accompanied by masks, that I would like to merge into a single string value/mask pair. The strings may have bytes that overlap but after applying masks, no overlapping bits should contradict what the value of that bit must be, i.e. value1 = 0x0A mask1 = 0xFE and value2 = 0x0B, mask2 = 0x0F basically says that the...

Comparing arbitrary bit sequences in a byte array in c

I have a couple uint8_t arrays in my c code, and I'd like to compare an arbitrary sequence bits from one with another. So for example, I have bitarray_1 and bitarray_2, and I'd like to compare bits 13 - 47 from bitarray_1 with bits 5-39 of bitarray_2. What is the most efficient way to do this? Currently it's a huge bottleneck in my pro...