bit-manipulation

Emulating variable bit-shift using only constant shifts?

I'm trying to find a way to perform an indirect shift-left/right operation without actually using the variable shift op or any branches. The particular PowerPC processor I'm working on has the quirk that a shift-by-constant-immediate, like int ShiftByConstant( int x ) { return x << 3 ; } is fast, single-op, and superscalar, whereas...

SPARC - Bit mask without shift

Hi, I'm learning SPARC assembly and I have to create a script that extracts a field from a register. The script accepts 3 values, initial number, field start position, field length. It can't use any shift functions, but it can use multiply and divide. I'm currently suffering from a respiratory virus, and am subsequently on a significant ...

java bit manipulation

byte x = -1; for(int i = 0; i < 8; i++) { x = (byte) (x >>> 1); System.out.println("X: " + x); } As I understand it, java stores data in two's-complement, meaning -1 = 11111111 (according to wikipedia). Also, from the java docs: "The bit pattern is given by the left-hand operand, and the number of positions to shift by the r...

Shift operations

I saw the following posted by one of the fellow stackoverflower and it sort of dumbfounds me. Would someone explain the shifting operations in the following code snippet: std::vector<bool> a; a.push_back(true); a.push_back(false); //... for (auto it = a.begin(); it != a.end();) // see 0x for meaning of auto { unsigned b = 0; f...

Why does this bitwise shift-right appear not to work?

Could someone explain to me why the mask is not shifted to the right at all? You can use anything in place of that 1 and the result will be the same. unsigned mask = ~0 >> 1; printf("%u\n", mask); ...

How to get checksums for strided patterns

I have a 64 bit number (but only the 42 low order bits are used) and need to computer the sum of the 4 bits at n, n+m, n+m*2 and n+m*3 (note: anything that can produce a sum >4 is invalid) for some fixed m and every value of n that places all the bits in the number as an example using m=3 and given the 16-bit number 0010 1011 0110 0001...

Convert char for bit data to integer in DB2

I'm writing a DB2 user-defined function for which I need an array of non-negative integers, which I represent as a varchar for bit data. I plan to use two bytes for each integer (giving me a maximum value of 2^16-1, which is acceptable). I can convert an integer to a char for bit data by using the chr function, but how do I get it back ...

Bit twiddling reorder

I need to do an arbitrary reorder of a 7 bit value (Yes I know I should be using a table) and am wondering if there are any bit hacks to do this. Example: // <b0, b1, b2, b3, b4, b5, b6> -> <b3, b2, b4, b1, b5, b0, b6> // the naive way out = (0x020 & In) << 5 | (0x008 & In) << 2 | (0x040 & In) | (0x012 & In) >> 1 | ...

Bit shifts in C

If the bit pattern corresponding to a signed integer is shifted to the right then 1 vacant bit will be filled by the sign bit 2 vacant bit will be filled by 0 3 The outcome is implementation dependent 4 none of the above The answer to this question is 3rd option.. Can anybody explain this,, Also give some basic idea, abou...

manipulation of fields in an array of bits in c++

So I'm wondering about both setting and manipulating bit fields. I've already found http://stackoverflow.com/questions/264552/cc-code-to-treat-a-character-array-as-a-bitstream which is similar to my question I guess but it doesn't doesn't give me a nice stl approach I am thinking has to exist. I was thinking of bitsets from the stl ...

Can someone explain this bit manipulation code?

I have a tree control with checkboxes next to each node that allows for checked, unchecked and middle checked states on the nodes. When clicking a node, the parent and children are updated. The code I found that does the trick uses bit shifting and I'm trying to understand what exactly is happening. Can someone explain the following cod...

What is the fastest/most efficient way to find the last set bit (msb) in an integer in C?

If I have some integer n, and I want to know the position of the last set bit (that is, if the least significant bit is on the right, I want to know the position of the furthest left bit that is a 1), what is the quickest/most efficient method of finding out? I know that POSIX supports a ffs() method in strings.h to find the first set b...

Find out number of bits needed to represent a positive integer in binary?

This is probably pretty basic, but to save me an hour or so of grief can anyone tell me how you can work out the number of bits required to represent a given positive integer in Java? e.g. I get a decimal 11, (1011). I need to get the answer, 4. I figured if I could work out how to set all the bits other than the most significant bit t...

Converting a range into a bit array

I'm writing a time-critical piece of code in C# that requires me to convert two unsigned integers that define an inclusive range into a bit field. Ex: uint x1 = 3; uint x2 = 9; //defines the range [3-9] // 98 7654 3 //must be converted to: 0000 0011 1111 1000 It may help to visualize the bits in ...

In Java, when using bitshifts, why does 1 << 32 != 1 << 31 << 1 ?

int a = 1 << 32; int b = 1 << 31 << 1; Why does a == 1? b is 0 as I expected. ...

Testing Bits To Create A String - Is there a better approach?

This code works, but I'm wondering if there is a better way to do it. Basically I need to test bits, and write the appropriate character or characters to a string depending on the state of the bit. The spaces are present because the characters will be displayed with a fixed width font and I'd like to keep them from moving around. C or...

How can I convert bits to bytes?

I have an array of 128 booleans that represent bits. How can I convert these 128 bit representations into 16 bytes? Example: I have an array that looks like this: 0110001100110000100010111011001011010011010001010001101101001100 1000010000000000001000111111111101000011111001111011111011111001 (Converted to 1s and 0s to be more concis...

Left bit shifting 255 (as a byte)

Can anyone explain why the following doesn't compile? byte b = 255 << 1 The error: Constant value '510' cannot be converted to a 'byte' I'm expecting the following in binary: 1111 1110 The type conversion has stumped me. ...

Best Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C

What is the best algorithm to achieve the following: 0010 0000 => 0000 0100 The conversion is from MSB->LSB to LSB->MSB. All bits must be reversed; that is, this is not endianness-swapping. ...

How can I tell if a number is a multiple of four using only the logic operator AND?

I'm messing with assembly language programming and I'm curious how I could tell if a number is a multiple of 4 using the logic operator AND? I know how to do it using "div" or "remainder" instructions but I'm trying to do this with bit manipulation of number/word. Can anyone point me in the right direction? I'm using MIPs but a Langua...