bit-manipulation

Rounding issues with bitwise C code

限于喜欢 提交于 2019-12-12 05:31:33
问题 I have to following bitwise code which casts a floating point value (packaged in an int) to an int value. Question: There are rounding issues so it fails in cases where input is 0x80000001 for example. How do I handle this? Here is the code: if(x == 0) return x; unsigned int signBit = 0; unsigned int absX = (unsigned int)x; if (x < 0) { signBit = 0x80000000u; absX = (unsigned int)-x; } unsigned int exponent = 158; while ((absX & 0x80000000) == 0) { exponent--; absX <<= 1; } unsigned int

How to do the bit manipulation when you don't know where the left-most '1' bit is?

为君一笑 提交于 2019-12-12 04:57:31
问题 0000 => 0000 0001 => 1111 0010 => 1110 0100 => 1100 1000 => 1000 Also: 0101 => 1101 As you can see I need to fill with '1's after the last '1', but I do not know in advance where the last '1' is. 回答1: Maybe something like unsigned int data; unsigned int copy = data; unsigned int shifts = 0; while (copy > 0) {copy=copy>>1; ++shifts;}; data = data|((1<<shifts)-1); 回答2: Instead of directly making the mask of the bits that have to be switched on, it's easier to make the mask of bits that should

How to read a 64-bit two's-complement integer from a binary file using R?

耗尽温柔 提交于 2019-12-12 04:55:09
问题 I gather that to represent a 64-bit integer in R I need to use a double. That's fine but I need to read such an integer from a binary file where it is stored as Big Endian 64-bit two's-complement (a java long). I can of course read the two signed integers in 4 byte chunks like so a = readBin(file, integer(), size=4, endian="big") b = readBin(file, integer(), size=4, endian="big") But how do I combine them in R to get the double I require? 回答1: It is definitely better to read it in as two

Large array of 26-bit unsigned integers

…衆ロ難τιáo~ 提交于 2019-12-12 04:03:57
问题 I need to work with a large array of 26-bit variables in RAM. It is too expensive to use 32-bit int s. Access should be as fast as possible (especially read operation). I came to the following scheme: each 26-bit value splits to three 8-bit values and one 2-bit value. #define N 500000000 uint8 arr1[N], arr2[N], arr3[N]; uint8 arr4[N / 4]; int read_value(int index) { int a1 = arr1[index]; // bits 0..7 int a2 = arr2[index]; // bits 8..15 int a3 = arr3[index]; // bits 16..23 int a4 = (arr4[index

Most efficient/easy method for permission system?

寵の児 提交于 2019-12-12 03:53:47
问题 I'm creating an app using CakePHP and have hit a mental barrier when trying to figure out a permission system for the app. I've narrowed it down to a couple different methods, and I'm looking for some information about which would be a) most easily implemented and b) most efficient (obviously there can be trade-off between these two). The app has many different models, but for simplification I'll just use User, Department, and Event. I want to be able to individually control CRUD permissions

counting the number of bit required to represent an integer in 2's complement

元气小坏坏 提交于 2019-12-12 03:49:11
问题 I have to write a function that count the number of bit required to represent an int in 2's complement form. The requirement: 1. can only use: ! ~ & ^ | + << >> 2. no loops and conditional statement 3. at most, 90 operators are used currently, I am thinking something like this: int howManyBits(int x) { int mostdigit1 = !!(0x80000000 & x); int mostdigit2 = mostdigit1 | !!(0x40000000 & x); int mostdigit3 = mostdigit2 | !!(0x20000000 & x); //and so one until it reach the least significant digit

How was this data conversion performed exactly?

风流意气都作罢 提交于 2019-12-12 03:46:43
问题 So, long story short, I am trying to 'unpack' some data myself, but I am not able to do so. Fortunately I have the solution, but I cannot figure out the method. Basically, I have this array of data here, [16 130 164 65 103 136 209 64 19 36 185 190 83] , and I am told that when 'unpacked' via 'fffB' , I get: [ 20.56350708,   6.54790068,  -0.36160335,  83] I know for a fact that this solution is correct, but I am not sure how we attained it. The context here is that the input array was

Java Bitwise AND operation between a double and int value [closed]

主宰稳场 提交于 2019-12-12 03:44:59
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have a latitude value as double and I want to perform a Bitwise AND operation on it followed by right shift of bits. Following is my line of code: pBuffer[1]=(latitude_decimal_degrees & 0xFF0000) >> 16; However

Generate all combinations in bit version

耗尽温柔 提交于 2019-12-12 03:37:41
问题 I'd like to generate all possible combination (without repetitions) in bit representation. I can't use any library like boost or stl::next_combination - it has to be my own code (computation time is very important). Here's my code (modified from ones StackOverflow user): int combination = (1 << k) - 1; int new_combination = 0; int change = 0; while (true) { // return next combination cout << combination << endl; // find first index to update int indexToUpdate = k; while (indexToUpdate > 0 &&

How to divide integer by a constant integer with right shift operators? [duplicate]

一个人想着一个人 提交于 2019-12-12 03:03:08
问题 This question already has answers here : How can I multiply and divide using only bit shifting and adding? (13 answers) Division by a constant using shifts and adds/subtracts (3 answers) Closed 6 years ago . I'm interested how to do this, because I just found out you can do integer multiplication easily, by using left shift operators: x * 25 = (x << 4) + (x << 3) + x Where the sum of base 2 powers equals to 25: 2^4+2^3+2^0 = 25 How does x / 25 work out with right shifts? Edit: I'm not going