bit-manipulation

Should I use #define, enum or const?

柔情痞子 提交于 2019-12-27 11:37:13
问题 In a C++ project I'm working on, I have a flag kind of value which can have four values. Those four flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each record I wish to keep this attribute, so I could use an enum: enum { xNew, xDeleted, xModified, xExisting } However, in other places in code, I need to select which records are to be visible to the user, so I'd like to be able to pass that as a single

Should I use #define, enum or const?

左心房为你撑大大i 提交于 2019-12-27 11:35:22
问题 In a C++ project I'm working on, I have a flag kind of value which can have four values. Those four flags can be combined. Flags describe the records in database and can be: new record deleted record modified record existing record Now, for each record I wish to keep this attribute, so I could use an enum: enum { xNew, xDeleted, xModified, xExisting } However, in other places in code, I need to select which records are to be visible to the user, so I'd like to be able to pass that as a single

Bit twiddling: which bit is set?

前提是你 提交于 2019-12-27 11:11:52
问题 I have a 64-bit unsigned integer with exactly 1 bit set. I'd like to assign a value to each of the possible 64 values (in this case, the odd primes, so 0x1 corresponds to 3, 0x2 corresponds to 5, ..., 0x8000000000000000 corresponds to 313). It seems like the best way would be to convert 1 -> 0, 2 -> 1, 4 -> 2, 8 -> 3, ..., 2^63 -> 63 and look up the values in an array. But even if that's so, I'm not sure what the fastest way to get at the binary exponent is. And there may be faster/better

Arbitrary precision bit manipulation (Objective C)

和自甴很熟 提交于 2019-12-25 18:12:40
问题 I need to do bit operations on representations of arbitrary precision numbers in Objective C. So far I have been using NSData objects to hold the numbers - is there a way to bit shift the content of those? If not, is there a different way to achieve this? 回答1: Using NSMutableData you can fetch the byte in a char , shift your bits and replace it with -replaceBytesInRange:withBytes: . I don't see any other solution except for writing your own date holder class using a char * buffer to hold the

How to add bits using bitwise?

陌路散爱 提交于 2019-12-25 12:09:31
问题 I was trying to figure out how to add bits (up to 2 bytes) using only the following bitwise operations: ~ & ^ | << >>. I've been trying for a while with no luck. I was wondering if anyone knew how. int logicalByteAdd(int x, int y) { return ; } 回答1: unsigned short add(unsigned short a, unsigned short b) { unsigned short carry = a & b; unsigned short result = a ^ b; while(carry != 0) { unsigned short shiftedcarry = carry << 1; carry = result & shiftedcarry; result ^= shiftedcarry; } return

Converting a range into a bit array

末鹿安然 提交于 2019-12-25 12:05:22
问题 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 reverse order The maximum value for this range is a parameter given at run-time which we'll call max_val . Therefore, the bit field variable ought to be defined as a UInt32 array with size equal to

Converting a range into a bit array

前提是你 提交于 2019-12-25 12:04:28
问题 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 reverse order The maximum value for this range is a parameter given at run-time which we'll call max_val . Therefore, the bit field variable ought to be defined as a UInt32 array with size equal to

Why didn't the complement's formula work?

好久不见. 提交于 2019-12-25 04:42:43
问题 I have just learnt that to get the formula to find the 1st Complement is -x = 2^n - x - 1 I have managed to apply it on a binary case: -00001100 (base 2) = 2^8 - 12 - 1 = 243 = 11110011 (1s) However, when I try to apply the same formula to a base 5 number, -1042 (base 4) = 5^4 - 1042 - 1 = 625 - 1042 - 1 = - 400 (which is not the answer) Can some one help me out here? Thanks 回答1: you cannot calculate any formula with numbers in 2 different bases, you have to use their decimal representation

Explain the following C++ code part

血红的双手。 提交于 2019-12-25 03:49:26
问题 //class start-- //Global variable static PMSK *savepr; static PRC *prs; //inside some method static PMSK wkpm; PMSK *pm; if (ipf) { k = to_bits(312, &msk); // This will return k=24 and msk =char(00000001), if ( pm->orbits[k] & msk ) // See the answer of my previous question. prs[i].pccused = 1; } For the to_bits method pls see the link Explain the following C++ method I am not familiar with C++ coding. What is goin on in the second if block? And explain the variable declarations also? Thanks

Convert between formats with different number of bits for exponent and fractional part

▼魔方 西西 提交于 2019-12-25 02:38:28
问题 I am trying to refresh on floats. I am reading an exercise that asks to convert from format A having: k=3, 4 bits fraction and Bias=3 to format B having k=4, 3 bits fraction and Bias 7. We should round when necessary. Example between formats: 011 0000 (Value = 1) =====> 0111 000 (Value = 1) 010 1001 (Value = 25/32) =====> 0110 100 (Value = 3/4 Rounded down) 110 1111 (Value = 31/2) =====> 1011 000 (Value = 16 Rounded up) Problem: I can not figure out how the conversion works. First of all I