bit-manipulation

Bitwise operations with big integers

自作多情 提交于 2019-12-05 01:30:34
I am implementing decoding of BER-compressed integers and recently I've found a weird JavaScript behavior related to bitwise operations with big integers. E.g.: var a = 17516032; // has 25 bits alert(a << 7) // outputs -2052915200 alert(a * 128) // outputs 2242052096 alert(2242052096 >> 16) // outputs -31325 alert(2242052096 / 65536) // outputs 34211 While the first workaround (multiplication instead of left shift) is acceptable, the second isn't. Why it happens? How to bear with it? 17516032 in binary is 00000001000010110100011000000000 . Shifting to the left by 7 gives you

Is it possible to check if any of 2 sets of 3 ints is equal with less than 9 comparisons?

Deadly 提交于 2019-12-05 00:34:48
int eq3(int a, int b, int c, int d, int e, int f){ return a == d || a == e || a == f || b == d || b == e || b == f || c == d || c == e || c == f; } This function receives 6 ints and returns true if any of the 3 first ints is equal to any of the 3 last ints. Is there any bitwise-hack similar way to make it faster? Expanding on dawg's SSE comparison method, you can combine the results of the comparisons using a vector OR, and move a mask of the compare results back to an integer to test for 0 / non-zero. Also, you can get data into vectors more efficiently (although it's still pretty clunky to

Rounded division by power of 2

时光怂恿深爱的人放手 提交于 2019-12-05 00:17:36
I'm implementing a quantization algorithm from a textbook. I'm at a point where things pretty much work, except I get off-by-one errors when rounding. This is what the textbook has to say about that: Rounded division by 2^p may be carried out by adding an offset and right-shifting by p bit positions Now, I get the bit about the right shift, but what offset are they talking about? Here's my sample code: def scale(x, power2=16): if x < 0: return -((-x) >> power2) else: return x >> power2 def main(): inp = [ 12595827, -330706, 196605, -387168, -274244, 377496, -241980, -545272, -196605, 24198,

set most significant bit in C

大城市里の小女人 提交于 2019-12-05 00:07:53
I am trying to set the most significant bit in a long long unsigned, x. To do that I am using this line of code: x |= 1<<((sizeof(x)*8)-1); I thought this should work, because sizeof gives size in bytes, so I multiplied by 8 and subtract one to set the final bit. Whenever I do that, the compiler has this warning: "warning: left shift count >= width of type" I don't understand why this error is occurring. The 1 that you are shifting is a constant of type int , which means that you are shifting an int value by sizeof(unsigned long long) * 8) - 1 bits. This shift can easily be more than the width

Converting rgba values into one integer in Javascript

大兔子大兔子 提交于 2019-12-04 23:33:19
I can already convert 32bit integers into their rgba values like this: pixelData[i] = { red: pixelValue >> 24 & 0xFF, green: pixelValue >> 16 & 0xFF, blue: pixelValue >> 8 & 0xFF, alpha: pixelValue & 0xFF }; But I don't really know how to reverse it. To reverse it, you just have to combine the bytes into an integer. Simply use left-shift and add them, and it will work. var rgb = (red << 24) + (green << 16) + (blue << 8) + (alpha); Alternatively, to make it safer, you could first AND each of them with 0xFF: var r = red & 0xFF; var g = green & 0xFF; var b = blue & 0xFF; var a = alpha & 0xFF; var

Compact a hex number

核能气质少年 提交于 2019-12-04 23:19:41
Is there a clever (ie: branchless) way to "compact" a hex number. Basically move all the 0s all to one side? eg: 0x10302040 -> 0x13240000 or 0x10302040 -> 0x00001324 I looked on Bit Twiddling Hacks but didn't see anything. It's for a SSE numerical pivoting algorithm. I need to remove any pivots that become 0. I can use _mm_cmpgt_ps to find good pivots, _mm_movemask_ps to convert that in to a mask, and then bit hacks to get something like the above. The hex value gets munged in to a mask for a _mm_shuffle_ps instruction to perform a permutation on the SSE 128 bit register. To compute mask for

One function with different arguments to push certain bits of an input integer to the left

℡╲_俬逩灬. 提交于 2019-12-04 22:00:47
This question is related to this . Let x be an 8 bit integer. I will number bits from left to right because that's how we read. If I want to get the third and fifth bits and put them in the first and second bits, with everything else as zero, I can have f(x) = (5*x) & 0b11000000 . More concisely: 00a0b000 -> ab000000 | f_0b00101000(x) = (5*x) & 0b11000000 However if I want the fifth, sixth and eighth bits to be in the first three bits, f(x) is different: 000ab0cd -> abcd0000 | f_0b00011011(x) = ((x << 3) & 0b11000000) | (x << 4) Note that the n in f_n(x) indicates which bits I care about. n

c++ bitstring to byte

送分小仙女□ 提交于 2019-12-04 21:15:21
For an assignment, I'm doing a compression/decompression of Huffman algorithm in Visual Studio. After I get the 8 bits ( 10101010 for example) I want to convert it to a byte. This is the code I have: unsigned byte = 0; string stringof8 = "11100011"; for (unsigned b = 0; b != 8; b++){ if (b < stringof8.length()) byte |= (stringof8[b] & 1) << b; } outf.put(byte); First couple of bitstring are output correctly as a byte but then if I have more than 3 bytes being pushed I get the same byte multiple times. I'm not familiar with bit manipulation and was asking for someone to walk me through this or

How to use bitwise operators to return a 0 or 1

☆樱花仙子☆ 提交于 2019-12-04 21:08:18
My function takes in a 32 bit int and I need to return a 0 or 1 if that number has a 1 in any even position. I cant use any conditional statements I also can only access 8 bits at a time. Here is an example input: 10001000 01011101 00000000 11001110 1) Shift the bits and and them with AA(10101010) and store each one in a variable. int a = 10001000 int b = 1000 int c = 0 int d = 10001010 Now I need to return a 0 if there were no odd bits set and 1 if there were. As we can see there were. So I need to combine these into one number and then use the !! operator to return 0 or 1. This is where I am

Fast bit shift of a byte array - CMAC subkeys

邮差的信 提交于 2019-12-04 20:59:39
问题 I need to implement as fast as possible left bit shift of a 16-byte array in JavaCard . I tried this code: private static final void rotateLeft(final byte[] output, final byte[] input) { short carry = 0; short i = (short) 16; do { --i; carry = (short)((input[i] << 1) | carry); output[i] = (byte)carry; carry = (short)((carry >> 8) & 1); } while (i > 0); } Any ideas how to improve the performace? I was thinking about some Util.getShort(...) and Util.setShort(...) magic, but I did not manage to