bit-manipulation

Bitwise operations in T-SQL

I have a bitmasked int field in my database. Usually I manage it through c# code, but now I need to flip a bit in the mask using T-SQL How do I accomplish the following: The bit i want to flip: 1 << 8 (256) The mask value before i flip: 143 The mask value after i flip: 399 I'm not exactly a bit-flipping wizard but this c...

How do you pass information from an 8 byte array into variable bit size data containers?

I have an 8 byte message where the differing chunks of the message are mapped to datums of different types (int, bool, etc.), and they vary in bit sizes (an int value is 12 bits in the message, etc.). I want to pass only the bits a datum is concerned with, but I am not sure if there is a better way. My current thoughts is to make a bit...

Fast Euclidean division in C

I am interested in getting the remainder of the Euclidean division, that is, for a pair of integers (i, n), find r such as: i = k * n + r, 0 <= r < |k| the simple solution is: int euc(int i, int n) { int r; r = i % n; if ( r < 0) { r += n; } return r; } But since I need to execute this tens of million o...

Managing bit packed data using C#

I'm working on a TCP based application that processes bitpacked messages, meaning: The messages transmitted/received are not byte aligned. For instance 3 bits represent field 1, where 19 bits may represent field 2. My question is, does anyone know of a C# library that can take a set of bytes and set/get an arbitrary range of bits with...

How to add two numbers without using ++ or + or another arithmetic operator.

How to add two numbers without using ++ or + or any other arithmetic operator. It was a question asked a long time ago in some campus-interview. Anyways today someone asked a question regarding some bit-manipulations, and in answers a beautiful quide *stanford Bit Twiddling * was referred. I spend some time studying it, and thought that ...

Is shifting bits faster than multiplying and dividing in Java? .NET?

Shifting bits left and right is apparently faster than multiplication and division operations on most (all?) CPUs if you happen to be using a power of 2. However, it can reduce the clarity of code for some readers and some algorithms. Is bit-shifting really necessary for performance, or can I expect the compiler/VM to notice the case and...

Bits representation of negative numbers

Hi, This is a doubt regarding the representation of bits of signed integers. For example, when you want to represent -1, it is equivalent to 2's complement of (+1). So -1 is represented as 0xFFFFFFF. Now when I shift my number by 31 and print the result it is coming back as -1. signed int a = -1; printf(("The number is %d ",(a>>31));//...

Swap bits in a number in C.

In a C interview, I was asked to swap the first 4-bits of a number with the last 4 bit. (eg. 1011 1110 should be 1110 1011.) Does anyone have a solution for this? ...

Using 'color' to define object's flag status?

I'm asking this question because it isn't the first time I saw this coding practice, but never saw any commentary about the reason for this: I was browsing the Lua's source and saw that they use 'colors' (white, black) to describe the state of an object. Here is the code from header lgc.h: /* ** Layout for bit use in `marked' field: ** ...

Pad a C++ structure to a power of two

I'm working on some C++ code for an embedded system. The I/O interface the code uses requires that the size of each message (in bytes) is a power of two. Right now, the code does something like this (in several places): #pragma pack(1) struct Message { struct internal_ { unsigned long member1; unsigned long member2; ...

What is the best way to explain bit manipulation to the students?

I am currently planning to conduct a Training Workshop on Optimized C Code for Microcontroller Applications. Currently I am stuck on how to explain bit manipulation in C? Exactly what I don't want is to lead students to bit structure but guide them to use bit manipulation efficiently... Any advice? ...

Convert Decimal to Hex when no datatype can hold the full number

This is an almost exact duplicate of my own question a few weeks ago. http://stackoverflow.com/questions/1143302/convert-hex-to-decimal-when-no-datatype-can-hold-the-full-number This time, it is the reverse. I have the number (in a handy null terminated string) and I need the bytes that make this number. However, I am working in a 32 b...

Image bit depth values.

REPHRASED QUESTION: I am coming up with a list of possible image bit depth values that could be used as a predefined reference list in my application. I could think of 8,16,24 and 32 bit depths. The image formats considered are BMP, JPEG, PNG and GIF. I understand the bit depth decides the quality and thereby the storage requirements ...

Bit shifting N bits

Hello quick question regarding bit shifting I have a value in HEX = new byte[] { 0x56, 0xAF }; which is 0101 0110 1010 1111 i want to the first n bits, example 12 then shift off the remaining 4 (16-12) to get 0000 0101 0110 1010 (1386 dec) i cant wrap my head around it and make it scalable for n bits. Thanks! ...

Finding a bit pattern in a 32bit unsigned integer.

I would describe my question using an example Given a source - 1010 0010, I would like to know how many times (count) the pattern 10 is present in the byte (the source can be of any size 8, 16, 24, or 32 bits). I would like the function which finds the count to be generic. User should be able to give his own pattern viz. 1000, 101 etc...

Fast Saturate and shift two Halfwords in ARM asm

I have two signed 16-bit values in a 32-bit word, and I need to shift them right (divide) on constant value (it can be from 1 to 6) and saturate to byte (0..0xFF). For example, 0x FFE1 00AA with shift=5 must become 0x 0000 0005; 0x 2345 1234 must become 0x 00FF 0091 I'm trying to saturate the values simultaneously, something like th...

Help me improve this C++ bit-buffer processing code

I am writing a function to process an incoming 32-bit buffer, representing changing data when it is compared to an corresponding stored 32-bit buffer. The position of the changing bit represents a number (i.e. a value of 8 means bit 3) that needs to be processed, along with whether the change is 0->1, or 1->0. Here is the current impleme...

interleaving bits with a mask

inputs: arbitrary bitset, e.g. bit positions 012345 arbitrary bit mask, e.g. (x=1) xx0x0x output: xx0x1x2345 That is, I want the first bit of the bitset to be placed in the first 0 of the mask. Likewise, the second bit is placed in the second 0 of the mask. example: mask = 1001001 bits = 1101 result = 1111011 I know that this ...

C# int to byte[]

If I need to convert an int to byte[] I could use Bitconvert.GetBytes(). But if I should follow this: An XDR signed integer is a 32-bit datum that encodes an integer in the range [-2147483648,2147483647]. The integer is represented in two's complement notation. The most and least significant bytes are 0 and 3, resp...

Bit manipulation library for ANSI C

Does anyone knows a good bit manipulation library for ANSI C? What I basically need, is ability, like in Jovial to set specific bits in a variable, something like // I assume LSB has index of 0 int a = 0x123; setBits(&a,2,5, 0xFF); printf("0x%x"); // should be 0x13F int a = 0x123; printf("0x%x",getBits(&a,2,5)); // should be 0x4 char ...