bit-manipulation

Bitwise operations in Visual Basic .NET

Hello guys, I'm re-writing some old application in VB.NET to C# and ASP.NET 3.5. Everything is going OK but I have this problem - and, as the database will not be changed, I must find a solution to it. The old app saves the list o desired days (from Sunday to Saturday) in a byte. This is the way it do it: If chkDaily.Checked Then ...

clear all but the two most significant set bits in a word

Given an 32 bit int which is known to have at least 2 bits set, is there a way to efficiently clear all except the 2 most significant set bits? i.e. I want to ensure the output has exactly 2 bits set. What if the input is guaranteed to have only 2 or 3 bits set.? Examples: 0x2040 -> 0x2040 0x0300 -> 0x0300 0x0109 -> 0x0108 0x5040 -> ...

What does the C compiler do with bitfields?

I'm working on an embedded project (PowerPC target, Freescale Metrowerks Codewarrior compiler) where the registers are memory-mapped and defined in nice bitfields to make twiddling the individual bit flags easy. At the moment, we are using this feature to clear interrupt flags and control data transfer. Although I haven't noticed any bu...

Optimizing quadrant selection

I'm working on a data structure which subdivides items into quadrants, and one of the bottlenecks I've identified is my method to select the quadrant of the point. Admittedly, it's fairly simple, but it's called so many times that it adds up. I imagine there's got to be an efficient way to bit twiddle this into what I want, but I can't t...

Finding the length of the common prefix in two bytes

Given two bytes, how would I find the length of the common bits at the start of the two bytes. For example: 9 == 00001001 6 == 00000110 Common prefix is 0000, length 4 I'm working in C#, so please stick to C# operations only. Addendum: This particular piece of code will run thousands of times and needs to be very fast. ...

PHP shift right

hey there i am trying to implement DataOutputStream in php (DataOutputStream from java language) in java code they shift right variables like this >>> in php i can only shift like this >> how can i do this in php ? thank you ...

Retain Data from 27 bit to 19 bit conversion

I have an access control solution where the 27 bit format is 13 bits for the facility code and 14 bits for the badge ID. Conversely, I need to convert it into 8 bits for the facility code and 16 bits for the badge ID. What is the largest number I can convert from on the 27 bit side to get the same result using the 8 bit facility code s...

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 start_bit); and that it may be called several times in quick succession on the same buffer....

Maximum value of unsigned char

#include <stdio.h> int main() { unsigned char i=0x80; printf("%d",i<<1); return 0; } Why does this program print 256? As I understand this, since 0x80= 0b10000000, and unsigned char has 8 bits, the '1' should overflow after left shift and the output should be 0, not 256. ...

Best way to overwrite some bits in a particular range

Given a series of bits, what's the best way to overwrite a particular range of them. For example, given: 0100 1010 Say I want to overwrite the middle 2 bits with 10 to make the result: 0101 0010 What would be the best way of doing this? At first, I thought I would just shift the overwriting bits I want to the correct position (1000...

What does "|=" mean in Java?

Note my question is not regarding != but |= A usage example is here I assume that x |= y is the same as x = x | y but I could not find confirming documentation and wanted to be sure Thanks ...

Simple way to set/unset an individual bit

Right now I'm using this to set/unset individual bits in a byte: if (bit4Set) nbyte |= (1 << 4); else nbyte &= ~(1 << 4); But, can't you do that in a more simple/elegant way? Like setting or unsetting the bit in a single operation? Note: I understand I can just write a function to do that, I'm just wondering if I won't be reinv...

Performance of shift operations in Java vs. C++

What is the performance of Shift Operation in Java compared to C++ ...

bit pattern matching and replacing

Hi, I come across a very tricky problem with bit manipulation. As far as I know, the smallest variable size to hold a value is one byte of 8 bits. The bit operations available in C/C++ apply to an entire unit of bytes. Imagine that I have a map to replace a binary pattern 100100 (6 bits) with a signal 10000 (5 bits). If the 1st byte o...

Assign unsigned char to unsigned short with bit operators in ansi C

I know it is possible to assign an unsigned char to an unsigned short, but I would like to have more control how the bits are actually assigned to the unsigned short. unsigned char UC_8; unsigned short US_16; UC_8 = 0xff; US_16 = (unsigned char) UC_8; The bits from UC_8 are now placed in the lower bits of US_16. I need more control o...

converting a set of booleans to a number

int bits = 0; bool a = true, b = false, c = true; // 101 = 5 bits = bits | a << 2; bits = bits | b << 1; bits = bits | c; cout << bits; This is the code I am going to use to take a set of three booleans and convert it into an int for a switch statement. I have 8 cases based on the combined state of these 3 booleans. Am I doing thi...

x-y = x+¬y+1 problem

I am currently reading a book about "bit fiddling" and the following formula appears: x-y = x+¬y+1 But this doesn't seem to work. Example: x = 0100 y = 0010 x-y = 0010 ¬y = 1101 ¬y+1 = 1110 x+1110 = 10010 But 10010 != 0010... Where did I make a mistake (if any)? (The book is "Hacker's Delight" by Henry S. Warren.) ...

Implementing our own serializers in C# - tips and gotchas

We probably need to write our own serializers for the classes in our (largeish) C# app - BinarySerializer is too slow and verbose, and protobuf-net has problems with interface-based properties (of which we have loads). Does anyone have any good tips and/or warnings? I suspect we should be using BinaryWriter and BinaryReader, but we hav...

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 like the best way would be to convert 1 -> 0, 2 -> 1, 4 -> 2, 8 -> 3, ..., 2^63 -> 63 a...

How to perform bit shift without ("<<" || ">>") operator efficiently?

Hi, I am working on a OpenGL ES 2.0 shader and I have tightly packed data e.g. three 5-bit unsigned integers within a block of two bytes. To unpack this data I obviously need bit-shifting, but this is not supported in OpenGL ES Shading Language (see page 29 http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf) Consequently I ...