bit-manipulation

Smallest integer larger than lg N

I was reading somewhere that: The smallest integer larger than lg N is the number of bits required to represent N in binary, in the same way that the smallest integer larger than log10 N is the number of digits required to represent N in decimal. The Java statement for (lgN = 0; N > 0; lgN++, N /= 2) ; is a si...

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

Bit fields in Scala

How to simulate bit fields in Scala? The bit fields are used to access some bits of one type (like this in C link). I know it's possible to write with bit operators, but I think there a better way if not consider the performance. Thanks for every hint that might give. ...

How does a bit field work with character types?

struct stats { char top : 1; char bottom : 1; char side : 2; } MyStat; I have seen this format with integers but how does the above char bit field work and what does it represent? Thank You. ...

can anyone help me with bit manipulation. thanks!

I'm not looking for the answers to be given to me, just some direction on what I need to look at to get started. /* copyLSB - set all bits of result to least significant bit of x * Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000 * Legal ops: ! ~ & ^ | + << >> * Max ops: 5 * Rating: 2 */ int copyLSB(int x) { return 0; ...

Calculate ~100 by hand

If I print out ~100 in Java, I get -101. I'm trying to work this out by hand. The NOT (~) operator inverts all bits. 100 in binary is 00000000000000000000000001100100. inverting its bits gives: 11111111111111111111111110011011 Now what? How do I get -101? ...

Bits - shifts/ manipulation/ tricks

I have been trying to learn about bits and first place to learn about them is Wikipedia, so I read about it. I have been preparing for the interviews where I see lot of questions which are solved with bit tricks/manipulations/shift Few examples are if number is power of 2 arithmetic operations without using arithmetic operators T...

Leading zeros calculation with intrinsic function

I'm trying to optimize some code working in an embedded system (FLAC decoding, Windows CE, ARM 926 MCU). The default implementation uses a macro and a lookup table: /* counts the # of zero MSBs in a word */ #define COUNT_ZERO_MSBS(word) ( \ (word) <= 0xffff ? \ ( (word) <= 0xff? byte_to_unary_table[word] + 24 : \ byte_...

What is 0xFF and why is it shifted 24 times?

#define SwapByte4(ldata) \ (((ldata & 0x000000FF) << 24) | \ ((ldata & 0x0000FF00) << 8) | \ ((ldata & 0x00FF0000) >> 8) | \ ((ldata & 0xFF000000) >> 24)) What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24? ...