bitwise

Fastest way to loop every number with conditions

Given a 64 bit integer, where the last 52 bits to be evaluated and the leading 12 bits are to be ignored, what is the fastest way to loop every single combination of 7 bits on and all other bits off? Example: First permutation: 0[x57]1111111 Last permutation 00000000000011111110[x45] Where 0[xn] means n off (zero) bits. Speed is...

C# Why does 127 = this bit string?

Given this code which prints all the bits in an integer out: private string getBitLiteral(bool bitVal) { if (bitVal) { return ("1"); } else { return ("0"); } }   Int64 intThisHand = 127; for (int i = 64; i > 0; i--) { HttpContext.Current.Response.Write( getBitLi...

Sign extending from a constant bit width in C#

Guys, I have a value thats 5 bits in length. 4 bits determine the number and the 5th bit determines the sign, there by holding any value between -16 and +15. How can I accomplish sign extending from a constant bit width in C#? I know in C, I can use something like the follow to accomplish this: int x; // convert this from using 5 bits ...

Arithmetic Bit-shift on a signed integer

Hi ! I am trying to figure out how exactly arithmetic bit-shift operators work in C, and how it will effect signed 32 bit integer. To make things simple, lets say we work within one byte(8 bits) - x = 1101.0101 MSB[ 1101.0101 ]LSB Reading other posts on Stack & some websites, I found that: << will shit toward MSB(to the left, in my...

signed bitwise arithmetic in Python

I got stuck with the following Python code >>> a = 0xff >>> b = 1 << 8 >>> ~a & ~b -512 Why is it -512? In binary notation it should look like this: a 0 1111 1111 -> 255 b 01 0000 0000 -> 256 ~a 1 0000 0000 -> -256 ~b 10 1111 1111 -> -257 ~a&~b 00 0000 0000 -> 0 I expected 0 as with signed int...