bit-manipulation

Bitwise version of finding RGB in java

爷,独闯天下 提交于 2019-12-07 17:32:04
问题 I have the following method that gets a rgb value and classifies it using a smaller palette: private static int roundToNearestColor( int rgb, int nrColors ) { int red = ( rgb >> 16 ) & 0xFF; int green = ( rgb >> 8 ) & 0xFF; int blue = ( rgb & 0xFF ); red = red - ( red % nrColors ); green = green - ( green % nrColors ); blue = blue - ( blue % nrColors ); return 0xFF000000 | ( red << 16 ) | ( green << 8 ) | ( blue ); } The code that annoys me is red = red - ( red % nrColors ); green = green - (

QWORD shuffle sequential 7-bits to byte-alignment with SIMD SSE…AVX

[亡魂溺海] 提交于 2019-12-07 16:37:52
问题 I would like to know if the following is possible in any of the SIMD families of instructions. I have a qword input with 63 significant bits (never negative). Each sequential 7 bits starting from the LSB is shuffle-aligned to a byte, with a left-padding of 1 (except for the most significant non-zero byte). To illustrate, I'll use letters for clarity's sake. The result is only the significant bytes, thus 0 - 9 in size, which is converted to a byte array. In: 0|kjihgfe|dcbaZYX|WVUTSRQ|PONMLKJ

How to get most significant n bits from int in java

浪尽此生 提交于 2019-12-07 16:03:59
问题 I have an int, and I would like to get the 19 most significant bits in java. I tried all sorts of methods, none of them work. Can someone please help me? 回答1: From the 32 bit int, you want to keep the 19 most significant, so discard the 13 least; then you shift right by 13 bits, but have to get rid of the possible sign extension, by anding with a 19 bit pattern: (myint >> 13) & 0x7ffff 回答2: Adding to Bram's answer, you don't even need the AND if you use unsigned shift. myInt >>> 13; will give

Bit shifting of negative integer?

怎甘沉沦 提交于 2019-12-07 13:11:36
问题 I am trying to understand the bit shift operation >> on a negative integer. -2 >> 1 # -1 -3 >> 1 # -2 -5 >> 1 # -3 -7 >> 1 # -4 Can someone explain how this is done? I know it is related to Two's complement, but I can't related that to the shifting operation. 回答1: The full explanation is provided here Two's Complement binary for Negative Integers: Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then

Applications of a circular shift

邮差的信 提交于 2019-12-07 12:49:46
问题 I would like to know some examples of application of circular shifts. For example, a right shift on an unsigned integer would result in a division by two. Conversely, a left shift would result in a multiplication by 2. Are there any famous/interesting properties of a circular shift on binary numbers. Note: The example about the right/left shift is to illustrate an application of that particular operator. I am asking for similar examples for the circular shift operator/function. 回答1: Convert a

Gold Rader bit reversal algorithm

我只是一个虾纸丫 提交于 2019-12-07 11:30:05
问题 I am trying to understand this bit reversal algorithm. I found a lot of sources but it doesn't really explain how the pseudo-code works. For example, I found the pseudo-code below from http://www.briangough.com/fftalgorithms.pdf for i = 0 ... n − 2 do k = n/2 if i < j then swap g(i) and g(j) end if while k ≤ j do j ⇐ j − k k ⇐ k/2 end while j ⇐ j + k end for From looking at this pseudo-code, I don't understand why you would do swap g(i) and g(j) when the if statement is true . Also: what does

Which is the most efficient way to extract an arbitrary range of bits from a contiguous sequence of words?

天涯浪子 提交于 2019-12-07 11:27:43
问题 Suppose we have an std::vector , or any other sequence container (sometimes it will be a deque), which store uint64_t elements. Now, let's see this vector as a sequence of size() * 64 contiguous bits. I need to find the word formed by the bits in a given [begin, end) range, given that end - begin <= 64 so it fits in a word. The solution I have right now finds the two words whose parts will form the result, and separately masks and combines them. Since I need this to be as efficient as

Efficient z-order transformation in Fortran

蹲街弑〆低调 提交于 2019-12-07 11:22:31
For my current work on a grid generation algorithm I need an efficient way to transform three-dimensional coordinates to z-order (more precisely: three 4-Byte integers into one 8-Byte integer) and the other way round. This Wikipedia article describes it fairly well: Z-order curve . Since I am not a programmer, the solution I came up with does what it is supposed to do but might be quite naive using the mvbits intrinsic to do the bit interleaving explicitly: SUBROUTINE pos_to_z(i, j, k, zval) use types INTEGER(I4B), INTENT(IN) :: i, j, k INTEGER(I8B), INTENT(OUT) :: zval INTEGER(I8B) :: i8, j8,

Best way to store long binary (up to 512 bit) in C#

。_饼干妹妹 提交于 2019-12-07 10:36:30
问题 I'm trying to figure out the best way to store large binary (more than 96 bit) numbers in C# I'm building application that will automatically allocate workers for shifts. Shifts can be as short as 15 minutes (but this might be even smaller in the future). To avoid double-booking of workers, I plan to have binary map of their daily time: 24 hours separated in equal chunks (15 minutes) and every chunk has a flag (0 for free, 1 for busy) So when we try to give another shift to a worker, we can

Comparing arbitrary bit sequences in a byte array in c

倖福魔咒の 提交于 2019-12-07 10:20:10
问题 I have a couple uint8_t arrays in my c code, and I'd like to compare an arbitrary sequence bits from one with another. So for example, I have bitarray_1 and bitarray_2, and I'd like to compare bits 13 - 47 from bitarray_1 with bits 5-39 of bitarray_2. What is the most efficient way to do this? Currently it's a huge bottleneck in my program, since I just have a naive implementation that copies the bits into the beginning of a new temporary array, and then uses memcmp on them. 回答1: three words: