bit-manipulation

ARM assembly puzzle

First of all, I'm not sure if solution even exists. I spent more than a couple of hours trying to come up with one, so beware. The problem: r1 contains an arbitrary integer, flags are not set according to its value. Set r0 to 1 if r1 is 0x80000000, to 0 otherwise, using only two instructions. It's easy to do that in 3 instructions (th...

Fast 24-bit array -> 32-bit array conversion?

Quick Summary: I have an array of 24-bit values. Any suggestion on how to quickly expand the individual 24-bit array elements into 32-bit elements? Details: I'm processing incoming video frames in realtime using Pixel Shaders in DirectX 10. A stumbling block is that my frames are coming in from the capture hardware with 24-bit pixel...

Macros to set and clear bits

Im trying to write a few simple macros to simplify the task of setting and clearing bits which should be a simple task however I cant seem to get them to work correctly. #define SET_BIT(p,n) ((p) |= (1 << (n))) #define CLR_BIT(p,n) ((p) &= (~(1) << (n))) ...

bitwise AND in Javascript with a 64 bit integer

I am looking for a way of performing a bitwise AND on a 64 bit integer in JavaScript. JavaScript will cast all of its double values into signed 32-bit integers to do the bitwise operations (details here). ...

Question about sizeof. I want to reverse bits in number.

What will be equivalent of this in Java? for (i = (sizeof(num)*8-1); i; i--) num is given number, not array. I want to reverse bits in integer. ...

What is the difference between bit shifting and arithmetical operations?

int aNumber; aNumber = aValue / 2; aNumber = aValue >> 1; aNumber = aValue * 2; aNumber = aValue << 1; aNumber = aValue / 4; aNumber = aValue >> 2; aNumber = aValue * 8; aNumber = aValue << 3; // etc. Whats is the "best" way to do operations? When is better to use bit shifting? ...

Finding shared list IDs in a MySQL table using bitwise operands

I want to find items in common from the "following_list" column in a table of users: +----+--------------------+-------------------------------------+ | id | name | following_list | +----+--------------------+-------------------------------------+ | 9 | User 1 | 26,6,12,10,21,24,19,16 ...

Find three numbers appeared only once

In a sequence of length n, where n=2k+3, that is there are k unique numbers appeared twice and three numbers appeared only once. The question is: how to find the three unique numbers that appeared only once? for example, in sequence 1 1 2 6 3 6 5 7 7 the three unique numbers are 2 3 5. Note: 3<=n<1e6 and the number will range from 1 ...

Getting value of LSB from Hex (C code)

Hi - first post here :) I've got a code like this in C: unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07}; I need to extract it such that the final value is: 0xa1df7 I have only been able to extract and get it working if the hex values that have at least 1 zero: unsigned char const data[ ] = {0x0a, 0xd0, 0xf0, 0x07}; using...

How to optimize this simple function which translates input bits into words?

I have written a function which reads an input buffer of bytes and produces an output buffer of words where every word can be either 0x0081 for each ON bit of the input buffer or 0x007F for each OFF bit. The length of the input buffer is given. Both arrays have enough physical place. I also have about 2Kbyte free RAM which I can use for ...

is mysql index useful on column 'state' when only doing bit-operations on the column?

I have a lot of domain entities (stored in mysql) which undergo lots of different operations. Each operation is executed from a different program. I need to keep (flow)-state for these entities which I implemented in as a long field 'flowstate' used as a bitset. to query mysql for entities which have undergone a certain operation I do s...

Beautiful examples of bit manipulation

What are the most elegant or useful examples of bit manipulation you have encountered or used? Examples in all languages are welcome. ...

Bitwise operators and converting an int to 2 bytes and back again.

first time user, Hi guys! So hopefully someone can help.. My background is php so entering the word of lowend stuff like, char is bytes, which are bits.. which is binary values.. etc is taking some time to get the hang of ;) What im trying to do here is sent some values from an Ardunio board to openFrameWorks (both are c++). What this...

How do I unpack bits from a structure's stream_data in c code?

Ex. typedef struct { bool streamValid; dword dateTime; dword timeStamp; stream_data[800]; } RadioDataA; Ex. Where stream_data[800] contains: **Variable** **Length (in bits)** packetID 8 packetL 8 versionMajor 4 versionMinor 4 radioID 8 etc.. I need to write: void unpackData(radioDataA *strea...

Drawbacks of using an integer as a bitfield?

I have a bunch of boolean options for things like "acceptable payment types" which can include things like cash, credit card, cheque, paypal, etc. Rather than having a half dozen booleans in my DB, I can just use an integer and assign each payment method an integer, like so PAYMENT_METHODS = ( (1<<0, 'Cash'), (1<<1, 'Credit Card...

PHP: Combine Two 16-bit Integers into a 32-bit integer

Hello, I am trying to combine two integers in my application. By combine I mean stick one byte stream at the end of the other, not concatenate the strings. The two integers are passed from hardware that can't pass a 32 bit value directly, but passes two consecutive 16-bit values separately. Thanks, ...

Convert int bytestream to a float bytestream

Hello, I have a 32-bit integer. The bit stream is actually a bit stream for a 32-bit float (IEEE 754). I tried converting it with: unpack('f', $input); This generates a float, but it seems it is not the right number For example, if I pass in 1, I should be coming out with 1.4012984e-45, according to the IEEE754 converter, but I am c...

Number of zero bits in integer except leading zeros

If I have an integer in Java how do I count how many bits are zero except for leading zeros? We know that integers in Java have 32 bits but counting the number of set bits in the number and then subtracting from 32 does not give me what I want because this will also include the leading zeros. As an example, the number 5 has one zero bi...

test if a& b==0

I want to implement a program which tests if a&b==0 . It should return false if the two integers have at least 1 bit in the same location and true if they have 1 bit in different locations. Here is my code: import java.util.*; public class btest{ public static boolean testand(int a,int b){ int i=0; int k=0; int m=0; while(i<32){ k=...

Need help identifying and computing a number representation

I need help identifying the following number format. For example, the following number format in MIB: 0x94 0x78 = 2680 0x94 0x78 in binary: [1001 0100] [0111 1000] It seems that if the MSB is 1, it means another character follows it. And if it is 0, it is the end of the number. So the value 2680 is [001 0100] [111 1000], formatted p...