bit-manipulation

Interleave bits efficiently

孤者浪人 提交于 2019-12-22 03:46:36
问题 I need to make uint64_t out of 2 uint32_t interleaving the bits: if A=a0a1a2...a31 and B=b0b1...b31 , I need C= a0b0a1b1...a31b31 . Is there a way to do this efficiently? So far I've got only the naive approach with a for loop of 32 iterations, where each iteration does C|=((A&(1<<i))<<i)|((B&(1<<i))<<(i+1)) . I guess there should be some mathematical trick like multiplying A and B by some special number which results in interleaving their bits with zeros in the resulting 64-bit number, so

Obtaining bit representation of a float in C

若如初见. 提交于 2019-12-21 23:56:43
问题 I'm trying to use unions to obtain the bit representation of float values, my code is currently as follows: union ufloat { float f; unsigned u; }; int main( ) { union ufloat u1; u1.f = 3.14159f; printf("u1.u : %f\n", u1.u); However anything I try to print gets printed as 0.0000000, instead of as bits (such as 0001 0110, or something similar), what is wrong in my code? Note that preferebly I would like to use unions to achieve this. 回答1: There are a large number of ways to accomplish this.

Replace byte in a int

99封情书 提交于 2019-12-21 17:44:40
问题 A int is composed of 4 bytes. How could I replace one of those 4 bytes with a new byte. In other words I am looking for a method: int ReplaceByte(int index, int value, byte replaceByte) { // implementation } for example if I have the value FFFFFFFF (-1) and I will like to replace the byte 0 with 0A (10) then I will call the method as: ReplaceByte(0,-1,10) and I will like that method to return me FFFFFF0A Do I have to convert the int to a byte array then replace the byte I want then convert

Bit manipulation, permutate bits

╄→гoц情女王★ 提交于 2019-12-21 17:38:11
问题 I am trying to make a loop that loops through all different integers where exactly 10 of the last 40 bits are set high, the rest set low. The reason is that I have a map with 40 different values, and I want to sum all different ways ten of these values can be multiplied. (This is just out of curiosity, so it's really the "bitmanip"-loop that is of interest, not the sum as such.) If I were to do this with e.g. 2 out of 4 bits, it would be easy to set all manually, 0011 = 3, 0101 = 5, 1001 = 9,

Can the xor-swap be extended to more than two variables?

好久不见. 提交于 2019-12-21 17:23:11
问题 I've been trying to extend the xor-swap to more than two variables, say n variables. But I've gotten nowhere that's better than 3*(n-1) . For two integer variables x1 and x2 you can swap them like this: swap(x1,x2) { x1 = x1 ^ x2; x2 = x1 ^ x2; x1 = x1 ^ x2; } So, assume you have x1 ... xn with values v1 ... vn . Clearly you can "rotate" the values by successively applying swap: swap(x1,x2); swap(x2,x3); swap(x3,x4); ... swap(xm,xn); // with m = n-1 You will end up with x1 = v2 , x2 = v3 , ..

Bitwise flags and Switch statement?

谁都会走 提交于 2019-12-21 09:29:05
问题 I have the following code (example), and I'm really not comfortable with so many 'if' checks: public enum Flags { p1 = 0x01, // 0001 p2 = 0x02, // 0010 p3 = 0x04, // 0100 p4 = 0x08 // 1000 }; public static void MyMethod (Flags flag) { if ((flag & Flags.p1) == Flags.p1) DoSomething(); if ((flag & Flags.p2) == Flags.p2) DosomethingElse(); if ((flag & Flags.p3) == Flags.p3) DosomethingElseAgain(); if ((flag & Flags.p4) == Flags.p4) DosomethingElseAgainAndAgain(); } MyMethod(Flags.p1 | Flags.p3);

Extracting Values Across Byte Boundaries With Arbitrary Bit Positions and Lengths In C#

℡╲_俬逩灬. 提交于 2019-12-21 09:25:18
问题 I am currently working on a network tool that needs to decode/encode a particular protocol that packs fields into dense bit arrays at arbitrary positions. For example, one part of the protocol uses 3 bytes to represent a number of different fields: Bit Position(s) Length (In Bits) Type 0 1 bool 1-5 5 int 6-13 8 int 14-22 9 uint 23 1 bool As you can see, several of the fields span multiple bytes. Many (most) are also shorter than the built-in type that might be used to represent them, such as

How does this bit manipulation work in Java?

拥有回忆 提交于 2019-12-21 09:25:06
问题 I was looking into how Java counts bits set of an int. I had in my mind something simple like this (which I think is correct): public static int bitCount(int number){ final int MASK = 0x1; int count = 0; for(int i = 0; i < 32; i++){ if(((number >>> i) & MASK) == MASK){ count++; } } return count; } Instead I found a method that I absolutely have no idea what is doing (seems like magic to me): i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4))

How to get lg2 of a number that is 2^k

故事扮演 提交于 2019-12-21 09:13:56
问题 What is the best solution for getting the base 2 logarithm of a number that I know is a power of two ( 2^k ). (Of course I know only the value 2^k not k itself.) One way I thought of doing is by subtracting 1 and then doing a bitcount: lg2(n) = bitcount( n - 1 ) = k, iff k is an integer 0b10000 - 1 = 0b01111, bitcount(0b01111) = 4 But is there a faster way of doing it (without caching)? Also something that doesn't involve bitcount about as fast would be nice to know? One of the applications

Pad a C++ structure to a power of two

前提是你 提交于 2019-12-21 08:27:20
问题 I'm working on some C++ code for an embedded system. The I/O interface the code uses requires that the size of each message (in bytes) is a power of two. Right now, the code does something like this (in several places): #pragma pack(1) struct Message { struct internal_ { unsigned long member1; unsigned long member2; unsigned long member3; /* more members */ } internal; char pad[64-sizeof(internal_)]; }; #pragma pack() I'm trying to compile the code on a 64-bit Fedora for the first time, where