bit-manipulation

How can I perform arithmetic right shift in C in a portable way?

别等时光非礼了梦想. 提交于 2019-12-01 05:28:25
We are writing an emulator where we need sign propagating right shift. The emulated system uses 2's complement numbers. I read that the >> operator on signed integers in C is implementation defined. So I cannot rely on the fact it will result in the correct bit pattern in all platforms. This means I'll need to use bit manipulation to reproduce the arithmetic right shift, and I would want to avoid unnecessary branching if possible. EDIT: In response to a comment: "The missing bit is that OP needs to define what result is "correct" when the sign bit is set in x with x >> y" I basically want to

C - Need to compare `n` lowest bits of an int for equality

寵の児 提交于 2019-12-01 05:12:22
问题 C - Need to compare n lowest bits of an int for equality. I.e. n = 4; xxxx1001 == xxxx1001 (x is don't care) I.e. n = 2; xxxxxx01 == xxxxxx01 Can't think of a nice way to do it without using masks, =). 回答1: Create the mask from the number of bits: int mask = (1 << bits) - 1; Then you use that to compare the values: if ((a & mask) == (b & mask)) 回答2: If you really don't want to use masks (not that there is anything wrong with that!), then you could use a shift-left operator: if( a << m == b <<

Standard C++11 code equivalent to the PEXT Haswell instruction (and likely to be optimized by compiler)

混江龙づ霸主 提交于 2019-12-01 04:02:22
The Haswell architectures comes up with several new instructions. One of them is PEXT ( parallel bits extract ) whose functionality is explained by this image (source here ): It takes a value r2 and a mask r3 and puts the extracted bits of r2 into r1 . My question is the following: what would be the equivalent code of an optimized templated function in pure standard C++11, that would be likely to be optimized to this instruction by compilers in the future. Here is some code from Matthew Fioravante's stdcxx-bitops GitHub repo that was floated to the std-proposals mailinglist as a preliminary

Variable length integer encoding

孤街醉人 提交于 2019-12-01 03:58:52
问题 I am attempting to reverse engineer an LZ1/LZ77 decompression algorithm. The length of an area of the decode buffer/window to be output is encoded in the file as a variable length integer. I've read as much as I can about variable length integer encoding and the method being used in this case does not appear to be like any others I have seen. Perhaps to avoid patent issues or maybe just to obfuscate. The included code might not be quite complete but it is working on at least several files at

Efficient way to set first N or last N bits of __m256i to 1, the rest to 0

本小妞迷上赌 提交于 2019-12-01 03:53:38
How to set to 1 efficiently with AVX2 first N bits last N bits of __m256i , setting the rest to 0 ? These are 2 separate operations for tail and head of a bit range, when the range may start and end in the middle of __m256i value. The part of the range occupying full __m256i values is processed with all- 0 or all- 1 masks. The AVX2 shift instructions vpsllvd and vpsrlvd have the nice property that shift counts greater than or equal to 32 lead to zero integers within the ymm register. In other words: the shift counts are not masked, in contrast to the shift counts for the x86 scalar shift

Using a bitwise & inside an if statement

╄→尐↘猪︶ㄣ 提交于 2019-12-01 03:42:06
In C, I can write an if-statement if (firstInt & 1) but when I try and do the same in Java, the compiler tells me "incompatible types" and says I need a boolean instead of an int . Is there any way to write that C code in Java? Any of the following should work for you: if ((firstInt & 1) != 0) if ((firstInt & 1) > 0) if ((firstInt & 1) == 1) In C an integer expression can be used implicitly as a boolean expression (although I would argue that it is a bad idea), where zero is false and any non-zero value is true. In Java that is not allowed so you have to make the expression explicitly boolean

Why does Java mask shift operands with 0x1F?

懵懂的女人 提交于 2019-12-01 03:31:24
In Java: (0xFFFFFFFF << 1) = 0xFFFFFFFE = 0b1111111111111110 : : : (0xFFFFFFFF << 30) = 0xE0000000 = 0b1110000000000000 (0xFFFFFFFF << 30) = 0xC0000000 = 0b1100000000000000 (0xFFFFFFFF << 31) = 0x80000000 = 0b1000000000000000 However: (0xFFFFFFFF << 32) = 0xFFFFFFFF = 0b1111111111111111 Logically this makes no sense, but what I believe to be happening is Java performing an operation similar to: a << (b % Integer.SIZE) [edit, apparently:] a << (b & 0x1F) This applies to >> and >>> , too. Obviously shifting by >= 32 (in the case of an Integer) removes all data from the data-type, but there are

How can I perform arithmetic right shift in C in a portable way?

旧时模样 提交于 2019-12-01 03:30:54
问题 We are writing an emulator where we need sign propagating right shift. The emulated system uses 2's complement numbers. I read that the >> operator on signed integers in C is implementation defined. So I cannot rely on the fact it will result in the correct bit pattern in all platforms. This means I'll need to use bit manipulation to reproduce the arithmetic right shift, and I would want to avoid unnecessary branching if possible. EDIT: In response to a comment: "The missing bit is that OP

What is the fastest way to calculate the number of bits needed to store a number

若如初见. 提交于 2019-12-01 03:29:17
I'm trying to optimize some bit packing and unpacking routines. In order to do the packing I need to calculate the number of bits needed to store integer values. Here is the current code. if (n == -1) return 32; if (n == 0) return 1; int r = 0; while (n) { ++r; n >>= 1; } return r; You're looking to determine the integer log base 2 of a number (the l=highest bit set). Sean Anderson's "Bit Twiddling Hacks" page has several methods ranging from the obvious counting bits in a loop to versions that use table lookup. Note that most of the methods demonstrated will need to be modified a bit to work

How to insert zeros between bits in a bitmap?

别来无恙 提交于 2019-12-01 03:27:26
I have some performance-heavy code that performs bit manipulations. It can be reduced to the following well-defined problem: Given a 13-bit bitmap, construct a 26-bit bitmap that contains the original bits spaced at even positions . To illustrate: 0000000000000000000abcdefghijklm (input, 32 bits) 0000000a0b0c0d0e0f0g0h0i0j0k0l0m (output, 32 bits) I currently have it implemented in the following way in C: if (input & (1 << 12)) output |= 1 << 24; if (input & (1 << 11)) output |= 1 << 22; if (input & (1 << 10)) output |= 1 << 20; ... My compiler (MS Visual Studio) turned this into the following: