bit-manipulation

Extracting a bit-field from a signed number

Hello, I have signed numbers (2s complement) stored in 32-bit integers, and I want to extract 16-bit fields from them. Is it true that if I extract the low 16 bits from a 32-bit signed number, the result will be correct as long as the original (32-bit) number fits into 16 bits ? For positive numbers it is trivially true, and it seems t...

Counting, reversed bit pattern

I am trying to find an algorithm to count from 0 to 2n-1 but their bit pattern reversed. I care about only n LSB of a word. As you may have guessed I failed. For n=3: 000 -> 0 100 -> 4 010 -> 2 110 -> 6 001 -> 1 101 -> 5 011 -> 3 111 -> 7 You get the idea. Answers in pseudo-code is great. Code fragments in any language are welcome,...

When to use Bitwise Operators during webdevelopment?

Although I grasp the concept of Bitwise Operators, I can't say that I have come across many use cases during the webdevelopment process at which I had to resort to using Bitwise Operators. Do you use Bitwise Operators? Why do you use them? What are some example use cases? Please remember that this question is specifically intended fo...

C/C++ Code to treat a character array as a bitstream

I have a big lump of binary data in a char[] array which I need to interpret as an array of packed 6-bit values. I could sit down and write some code to do this but I'm thinking there has to be a good extant class or function somebody has written already. What I need is something like: int get_bits(char* data, unsigned bitOffset, unsi...

Explanation of an algorithm to set, clear and test a single bit

Hey, in the Programming Pearls book, there is a source code for setting, clearing and testing a bit of the given index in an array of ints that is actually a set representation. The code is the following: #include<stdio.h> #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000 int a[1+ N/BITSPERWORD]; void set(in...

Why this union's size is 2 with bitfields?

I am working on turbo C on windows where char takes one byte.Now my problem is with the below union. union a { unsigned char c:2; }b; void main() { printf("%d",sizeof(b)); \\or even sizeof(union a) } This program is printing output as 2 where as union should be taking only 1 byte. Why is it so? for struct it is fine giving 1 byte bu...

Clear upper 16-bits in 1 ARM instruction

In ARM assembly immediates are encoded by an 8-bit rotated value which means we can only encode (0-256)^2n. Now my problem is that I want to clear the upper 16-bits of r0 and replace it with the half-word stored r1. But because of the limited range of the immediates I have to do: - bic r0, r0, #0xff000000 bic r0, r0, #0x00ff0000 add...

Bit mask in C

What is the best way to construct a bit mask in C with m set bits preceded by k unset bits, and followed by n unset bits: 00..0 11..1 00..0 k m n For example, k=1, m=4, n=3 would result in the bit mask: 01111000 ...

Stored procedure to modify bit flag, can't use Enums since many apps will be modifying it, what should I do?

Hi, There is a column in a database that is of type INT (Sql server). This int value is used at a bit flag, so I will be AND'ing and OR'ing on it. I have to pass a parameter into my sproc, and that parameter will represent a specific flag item. I would normally use an enumeration and pass the int representation to the sproc, but sinc...

How do you implement XOR using +-*/ ?

How can the XOR operation (on two 32 bit ints) be implemented using only basic arithmetic operations? Do you have to do it bitwise after dividing by each power of 2 in turn, or is there a shortcut? I don't care about execution speed so much as about the simplest, shortest code. Edit: This is not homework, but a riddle posed on a hacker....

Why does the BitConverter return Bytes and how can I get the bits then?

As input I get an int (well, actually a string I should convert to an int). This int should be converted to bits. For each bit position that has a 1, I should get the position. In my database, I want all records that have an int value field that has this position as value. I currently have the following naive code that should ask my enti...

Python: Set Bits Count (popcount)

Few blob's have been duplicated in my database(oracle 11g), performed XOR operations on the blob using UTL_RAW.BIT_XOR. After that i wanted to count the number of set bits in the binary string, so wrote the code above. During a small experiment, i wanted to see what is the hex and the integer value produced and wrote this procedure.. ...

Fastest way to clamp a real (fixed/floating point) value?

Hi, Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be handled in separate functions. Obviously, I can do something like: double clampedA; ...

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 ...

(VB6) Convert a 32bit Long to a String*6 [A-Z].

Hello I wish to convert a 32bit Long to a String*6 which will only use the characters [A-Z]. Using VB6 (don't ask!). I've calculated that a single letter uses 5 bits, so I can get 6 letters out of a 32 bit long. Can anyone give me a pointer on how to do this, as I have no clue. ...

Finding the next in round-robin scheduling by bit twiddling

Consider the following problem. You have a bit-string that represents the current scheduled slave in one-hot encoding. For example, "00000100" (with the leftmost bit being #7 and rightmost #0) means that slave #2 is scheduled. Now, I want to pick the next scheduled slave in a round-robin scheduling scheme, with a twist. I have a "reque...

Templatized branchless int max/min function

I'm trying to write a branchless function to return the MAX or MIN of two integers without resorting to if (or ?:). Using the usual technique I can do this easily enough for a given word size: inline int32 imax( int32 a, int32 b ) { // signed for arithmetic shift int32 mask = a - b; // mask < 0 means MSB is 1. return a +...

Have you ever had to use bit shifting in real projects?

Have you ever had to use bit shifting in real programming projects? Most (if not all) high level languages have shift operators in them, but when would you actually need to use them? ...

C/C++ check if one bit is set in, i.e. int variable

int temp = 0x5E; // in binary 0b1011110. Is there such a way to check if bit 3 in temp is 1 or 0 without bit shifting and masking. Just want to know if there is some built in function for this, or am I forced to write one myself. ...

Using Bitwise operators on flags

I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All = 0x7 Say I receive the two flags Past and Future (setFlags(PAST | FUTURE)). How can I tell if Past is in it? Likewise how can I tell that Current is not in it? That way I don't have to test for every possible combination. ...