bit-manipulation

How can an (int) be converted to (unsigned int) while preserving the original bit pattern?

為{幸葍}努か 提交于 2021-02-07 07:11:11
问题 Suppose that we define: short x = -1; unsigned short y = (unsigned short) x; According to the C99 standard: Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (ISO/IEC 9899:1999 6.3.1.3/2) So, assuming two bytes for short and a two's complement representation, the bit patterns of these two integers are: x = 1111 1111 1111 1111

What's the fastest way to do a right bit rotation/circular shift on a byte array

[亡魂溺海] 提交于 2021-02-07 05:45:21
问题 If I have the array: {01101111,11110000,00001111} // {111, 240, 15} The result for a 1 bit shift is: {10110111,11111000,00000111} // {183, 248, 7} The array size is not fixed, and the shifting will be from 1 to 7 inclusive. Currently I have the following code (which works fine): private static void shiftBitsRight(byte[] bytes, final int rightShifts) { assert rightShifts >= 1 && rightShifts <= 7; final int leftShifts = 8 - rightShifts; byte previousByte = bytes[0]; // keep the byte before

Fast method to multiply integer by proper fraction without floats or overflow

一曲冷凌霜 提交于 2021-02-07 05:14:54
问题 My program frequently requires the following calculation to be performed: Given: N is a 32-bit integer D is a 32-bit integer abs(N) <= abs(D) D != 0 X is a 32-bit integer of any value Find: X * N / D as a rounded integer that is X scaled to N/D (i.e. 10 * 2 / 3 = 7) Obviously I could just use r=x*n/d directly but I will often get overflow from the x*n . If I instead do r=x*(n/d) then I only get 0 or x due to integer division dropping the fractional component. And then there's r=x*(float(n)/d)

Fast method to multiply integer by proper fraction without floats or overflow

痞子三分冷 提交于 2021-02-07 05:14:38
问题 My program frequently requires the following calculation to be performed: Given: N is a 32-bit integer D is a 32-bit integer abs(N) <= abs(D) D != 0 X is a 32-bit integer of any value Find: X * N / D as a rounded integer that is X scaled to N/D (i.e. 10 * 2 / 3 = 7) Obviously I could just use r=x*n/d directly but I will often get overflow from the x*n . If I instead do r=x*(n/d) then I only get 0 or x due to integer division dropping the fractional component. And then there's r=x*(float(n)/d)

What do (n&(n-1))==0 and n&(n-1)==0 do in C++?

為{幸葍}努か 提交于 2021-02-06 14:19:31
问题 What do (n&(n-1))==0 and n&(n-1)==0 do ( n is an integer) in C++? if ((n&(n-1))==0) { // do something } if (n&(n-1)==0) { // do something } 回答1: (n & (n - 1)) == 0 : n & (n - 1) unset the lower set bit of n in binary: XXX10000 -> XXX00000 So (n & (n - 1)) == 0 for 0 and all powers of 2 . n & (n - 1) == 0 : n & (n - 1) == 0 is equivalent to n & ((n - 1) == 0) (due to precedence of operator) and so n == 1 . 回答2: They are suppose to test if the number n is a power of 2 (although the second one

What do (n&(n-1))==0 and n&(n-1)==0 do in C++?

妖精的绣舞 提交于 2021-02-06 14:18:59
问题 What do (n&(n-1))==0 and n&(n-1)==0 do ( n is an integer) in C++? if ((n&(n-1))==0) { // do something } if (n&(n-1)==0) { // do something } 回答1: (n & (n - 1)) == 0 : n & (n - 1) unset the lower set bit of n in binary: XXX10000 -> XXX00000 So (n & (n - 1)) == 0 for 0 and all powers of 2 . n & (n - 1) == 0 : n & (n - 1) == 0 is equivalent to n & ((n - 1) == 0) (due to precedence of operator) and so n == 1 . 回答2: They are suppose to test if the number n is a power of 2 (although the second one

How can I store 4 8 bit coordinates into one integer (C#)?

牧云@^-^@ 提交于 2021-02-05 09:40:57
问题 Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these values is 8 bits at max and I want to store them into one integer (32 bits), how can I achieve this? By doing this, I want to create a dictionary, keeping count of how often certain states have happened in the game. For example, 5, 10, 20, 12 is one state, 6, 10, 20, 12 would be another. 回答1: You can use BitConverter To

How can I store 4 8 bit coordinates into one integer (C#)?

老子叫甜甜 提交于 2021-02-05 09:40:52
问题 Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these values is 8 bits at max and I want to store them into one integer (32 bits), how can I achieve this? By doing this, I want to create a dictionary, keeping count of how often certain states have happened in the game. For example, 5, 10, 20, 12 is one state, 6, 10, 20, 12 would be another. 回答1: You can use BitConverter To

How can I store 4 8 bit coordinates into one integer (C#)?

折月煮酒 提交于 2021-02-05 09:40:41
问题 Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these values is 8 bits at max and I want to store them into one integer (32 bits), how can I achieve this? By doing this, I want to create a dictionary, keeping count of how often certain states have happened in the game. For example, 5, 10, 20, 12 is one state, 6, 10, 20, 12 would be another. 回答1: You can use BitConverter To

Using Bit Fields to save memory

风流意气都作罢 提交于 2021-02-05 08:48:06
问题 I am currently starting a project on the ps3 at university and we get marks for how optimized the code is. Me and my partner have been looking at bit fields as we are dealing with millions of numbers between 0 and 255. We figured if we can pack 4 integers into 4 bytes(typical integer sized block of memory) instead of just one then we can quarter the memory used. We see dealing with the data to be one of the largest optimisations we could make and we are looking into everything. Is it worth