bit-manipulation

Calculating rgb vals for BufferedImage

I am using following snippet to build a 32 bit integer to use with setRGB of BufferedImage (bit-or (bit-shift-left a 24) (bit-or (bit-shift-left r 16) (bit-or (bit-shift-left g 8) b))) after writing colors reading them back reveals wrong colors is there a fault in my logic? ...

What are the best methods to "Clear the 6th bit" of an integer?

What are the best methods to "Clear the 6th bit" of an integer? And, is your solution platform independent? (32 or 64 bit integer, etc). If not, can you give a solution that is platform independent? Update: we don't know whether that bit is set or unset when it was given... also, any language is ok... i know of a solution that is p...

Alternative to BitConverter.ToInt32

I'm using BitConverter.ToInt32 to pack 3 byte values into an int, like so: byte R = 0; byte G = 0; byte B = 0; int i = BitConverter.ToInt32(new byte[] { R, G, B, 0 }, 0); Is there a faster way to do this that doesn't involve the creation of a new int each time? Getting the bytes out of an int is easy: int i = 34234; byte B = (byte)(...

Why is abs(0x80000000) == 0x80000000?

I just started reading Hacker's Delight and it defines abs(-231) as -231. Why is that? I tried printf("%x", abs(0x80000000)) on a few different systems and I get back 0x80000000 on all of them. ...

Mix of two bit sequences

Is there any clever way to mix two bit sequences in such way that bits from first sequence will be on odd places, and bits from second sequence will be on even places. Both sequences are no longer than 16b so output will fit into 32bit integer. Example: First sequence : 1 0 0 1 0 0 Second sequence : 1 1 1 0 1 ...

How do you return a string from a function correctly in Dynamic C?

I have a program I am trying to debug, but Dynamic C apparently treats strings differently than normal C does (well, character arrays, anyway). I have a function that I made to make an 8 character long (well, 10 to include the \0 ) string of 0s and 1s to show me the contents of an 8-bit char variable. (IE, I give it the number 13, it r...

Find most significant bit (left-most) that is set in a bit array

I have a bit array implementation where the 0th index is the MSB of the first byte in an array, the 8th index is the MSB of the second byte, etc... What's a fast way to find the first bit that is set in this bit array? All the related solutions I have looked up find the first least significant bit, but I need the first most significant...

Getting the fractional part of a float without using modf()

Hi, I'm developing for a platform without a math library, so I need to build my own tools. My current way of getting the fraction is to convert the float to fixed point (multiply with (float)0xFFFF, cast to int), get only the lower part (mask with 0xFFFF) and convert it back to a float again. However, the imprecision is killing me. I'm...

In C/C++ what's the simplest way to reverse the order of bits in a byte?

While there are multiple ways to reverse bit order in a byte, I'm curious as to what is the "simplest" for a developer to implement. And by reversing I mean: 1110 -> 0111 0010 -> 0100 This is similar to, but not a duplicate of this PHP question. ...

Invert 1 bit in C#

I have 1 bit in a byte (always in the lowest order position) that I'd like to invert. ie given 00000001 I'd like to get 00000000 and with 00000000 I'd like 00000001. I solved it like this: bit > 0 ? 0 : 1; I'm curious to see how else it could be done. ...

Overwriting a range of bits in an integer in a generic way

Given two integers X and Y, I want to overwrite bits at position P to P+N. Example: int x = 0xAAAA; // 0b1010101010101010 int y = 0x0C30; // 0b0000110000110000 int result = 0xAC3A; // 0b1010110000111010 Does this procedure have a name? If I have masks, the operation is easy enough: int mask_x = 0xF00F; // 0b1111000000001...

Why is it useful to count the number of bits?

I've seen the numerous questions about counting the number of set bits in an insert type of input, but why is it useful? For those looking for algorithms about bit counting, look here: http://stackoverflow.com/questions/1517848/counting-common-bits-in-a-sequence-of-unsigned-longs http://stackoverflow.com/questions/472325/fastest-way-t...

Saturated addition of two signed Java 'long' values

How can one add two long values in Java so that if the result overflows then it is clamped to the range Long.MIN_VALUE..Long.MAX_VALUE? For adding ints one can perform the arithmetic in long precision and cast the result back to an int, e.g.: int saturatedAdd(int x, int y) { long sum = (long) x + (long) y; long clampedSum = Math.ma...

bit ordering and endianess

I am reading a file byte-by-byte. Say for example i have this byte: 0x41 (0100 0001) represented in hex. Now, I want the first three bits of this byte, i.e (010). I can use bitwise logic to extract the first three bits, but my question is will the first three bits be independent of endianess of the machine.(i.e they can't be 001)? Th...

Easiest way to find the correct kademlia bucket

In the Kademlia protocol node IDs are 160 bit numbers. Nodes are stored in buckets, bucket 0 stores all the nodes which have the same ID as this node except for the very last bit, bucket 1 stores all the nodes which have the same ID as this node except for the last 2 bits, and so on for all 160 buckets. What's the fastest way to find wh...

Fastest way to calculate an X-bit bitmask?

I have been trying to solve this problem for a while, but couldn't with just integer arithmetic and bitwise operators. However, I think its possible and it should be fairly easy. What am I missing? The problem: to get an integer value of arbitrary length (this is not relevant to the problem) with it's X least significant bits sets to 1 ...

Iterate through all possible floating-point values

How can I list all possible values of a floating-point data type? I can do this using a union in C or C++ but will that be portable? How can this be done in other languages? Javascript? Let's just assume I am using this iteration to map theta to sin(theta). ...

Setting last N bits in an array

I'm sure this is fairly simple, however I have a major mental block on it, so I need a little help here! I have an array of 5 integers, the array is already filled with some data. I want to set the last N bits of the array to be random noise. [int][int][int][int][int] eg. set last 40 bits [unchanged][unchanged][unchanged][24 bits of ...

Better name for CHAR_BIT?

I was just checking an answer and realized that CHAR_BIT isn't defined by headers as I'd expect, not even by #include <bitset>, on newer GCC. Do I really have to #include <climits> just to get the "functionality" of CHAR_BIT? ...

What's up with this reversing bit order function?

I'm rather ashamed to admit that I don't know as much about bits and bit manipulation as I probably should. I tried to fix that this weekend by writing some 'reverse the order of bits' and 'count the ON bits' functions. I took an example from here but when I implemented it as below, I found I had to be looping while < 29. If I loop while...