bit-manipulation

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

拟墨画扇 提交于 2019-12-04 01:15:39
问题 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. 回答1: Here is some code from Matthew

Why does Java mask shift operands with 0x1F?

守給你的承諾、 提交于 2019-12-04 00:18:29
问题 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.

what does it mean to bitwise left shift an unsigned char with 16

若如初见. 提交于 2019-12-04 00:18:17
i am reading a .cpp file containing a unsigned char variable, it's trying the bitwise left shift 16 bits, since an unsigned char is composed of 8 bits, left shift 16 bits will erase all the bits and fill it with eight 0s. unsigned char byte=0xff; byte << 16; When you shift a value, unsigned char x = ...; int y = x << 16; The type of x is promoted to int if unsigned char fits in an int (most systems), or to unsigned if unsigned char does not fit in an int (rare 1 ). As long as your int is 25 bits wide or wider, then no data will be discarded 2 . Note that this is completely unrelated to the

What does >> mean in PHP?

a 夏天 提交于 2019-12-03 23:28:50
Consider: echo 50 >> 4; Output: 3 Why does it output 3? nevets1219 50 in binary is 11 0010 , shift right by 4 yields 11 which is equal to 3. See PHP documentation and Wikipedia . As documented on php.org , the >> operator is a bitwise shift operator which shifts bits to the right: $a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two") 50 in binary is 110010 , and the >> operator shifts those bits over 4 places in your example code. Although this happens in a single operation, you could think of it in multiple steps like this: Step 1 - 00011001 Step 2 - 00001100

What does i+=(i&-i) do? Is it portable?

旧城冷巷雨未停 提交于 2019-12-03 23:18:45
Let i be a signed integer type. Consider i += (i&-i); i -= (i&-i); where initially i>0 . What do these do? Is there an equivalent code using arithmetic only? Is this dependent on a specific bit representation of negative integers? Source: setter's code of an online coding puzzle (w/o any explanation/comments). If i has unsigned type, the expressions are completely portable and well-defined. If i has signed type, it's not portable, since & is defined in terms of representations but unary - , += , and -= are defined in terms of values. If the next version of the C++ standard mandates twos

C# Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first

时间秒杀一切 提交于 2019-12-03 22:35:00
I know these warnings are probably pointless.. But anyway I could get rid of them? I got 7 of these warnings. Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first This has something to do with the OR operator | I highlighted what gives off the warnings. int result = (int)ror((uint)(v76 ^ (v75 | 0x862D63D3)), (uint)(BitConverter.ToInt32(v4, 72) ^ 0x22)); int v11 = (int)rol((uint)(int)((v8 & v10 | ~v10 & 0xEFCDAAC9) + v3[2] - 1126481991), 17); int v144 = (int)rol((uint)(int)((v141 & v143 | ~v143 & 0xEFCDAAC9) + v3[2] - 1126481991), 17); int v77 =

uint64 UTC time

自古美人都是妖i 提交于 2019-12-03 21:43:33
I have a UTC date time without the formatting stored in a uint64, ie: 20090520145024798 I need to get the hours, minutes, seconds and milliseconds out of this time. I can do this very easily by converting it to a string and using substring. However, this code needs to be very fast so I would like to avoid string manipulations. Is there faster way, perhaps using bit manipulation to do this? Oh by the way this needs to be done in C++ on Linux. uint64 u = 20090520145024798; unsigned long w = u % 1000000000; unsigned millisec = w % 1000; w /= 1000; unsigned sec = w % 100; w /= 100; unsigned min =

Multiplying using Bitwise Operators

与世无争的帅哥 提交于 2019-12-03 21:12:23
I was wondering how I could go about multiplying a series of binary bits using bitwise operators. However, I'm interested in doing this to find the a decimal fraction value for the binary value. Here's an example of what I'm trying to do: Given, say: 1010010, I want to use each individual bit so that it will be computed as: 1*(2^-1) + 0*(2^-2) + 1*(2^-3) + 0*(2^-4)..... Though I'm interested in doing this in ARM assembly, having an example in C/C++ will still help as well. I was thinking of performing a loop with a counter, where each time the loop iterates, a counter will increment, the value

Find min/max of a float/double that has the same internal representation

岁酱吖の 提交于 2019-12-03 19:34:36
问题 Refreshing on floating points (also PDF), IEEE-754 and taking part in this discussion on floating point rounding when converting to strings, brought me to tinker: how can I get the maximum and minimum value for a given floating point number whose binary representations are equal. Disclaimer : for this discussion, I like to stick to 32 bit and 64 bit floating point as described by IEEE-754. I'm not interested in extended floating point (80-bits) or quads (128 bits IEEE-754-2008) or any other

Removing lowest order bit

佐手、 提交于 2019-12-03 19:22:23
Given a binary number, what is the fastest way of removing the lowest order bit? 01001001010 -> 01001001000 It would be used in code to iterate over the bits of a variable. Pseudo-code follows. while(bits != 0){ index = getIndexOfLowestOrderBit(bits); doSomething(index); removeLowestOrderBit(bits); } The possible languages I'm considering using are C and Java. Uh ... In your example, you already know the bit's index. Then it's easy: bits &= ~(1 << index); This will mask off the bit whose index is index , regardless of its position in the value (highest, lowest, or in-between). Come to think of