bit-manipulation

Integer subtraction with wrap around for N bits

大憨熊 提交于 2019-11-30 21:44:22
Basically, the behavior you get when overflowing integers with subtraction, but for a given number of bits. The obvious way, assuming a signed integer: template <int BITS> int sub_wrap(int v, int s) { int max = (1<<(BITS)); v -= s; if (v < -max) v += max*2; // or if branching is bad, something like: // v += (max*2) * (v < -max) return v; } // For example subtracting 24 from -16 with 5 bit wrap, // with a range of -32, 31 sub_wrap<5>(-16, 28); -> 20 Is there a neat way of doing it that is less ugly and preferably faster than the one above? UPDATE: Sorry about the confusion. I thoughtlessly

Sign extension with bitwise shift operation

我怕爱的太早我们不能终老 提交于 2019-11-30 21:35:27
following this Q&A I tried to exam the answer so I wrote: #include <stdio.h> int main () { int t;int i; for (i=120;i<140;i++){ t = (i - 128) >> 31; printf ("t = %X , i-128 = %X , ~t & i = %X , ~t = %X \n", t, i-128 , (~t &i), ~t); } return 0; } and the Output is: t = FFFFFFFF , i-128 = FFFFFFF8 , ~t & i = 0 , ~t = 0 t = FFFFFFFF , i-128 = FFFFFFF9 , ~t & i = 0 , ~t = 0 t = FFFFFFFF , i-128 = FFFFFFFA , ~t & i = 0 , ~t = 0 t = FFFFFFFF , i-128 = FFFFFFFB , ~t & i = 0 , ~t = 0 t = FFFFFFFF , i-128 = FFFFFFFC , ~t & i = 0 , ~t = 0 t = FFFFFFFF , i-128 = FFFFFFFD , ~t & i = 0 , ~t = 0 t = FFFFFFFF

Efficient bit-fiddling in a LFSR implementation

旧城冷巷雨未停 提交于 2019-11-30 21:18:47
Although I have a good LSFR C implementation I thought I'd try the same in Haskell - just to see how it goes. What I came up with, so far, is two orders of magnitude slower than the C implementation, which begs the question: How can the performance be improved? Clearly, the bit-fiddling operations are the bottleneck, and the profiler confirms this. Here's the baseline Haskell code using lists and Data.Bits : import Control.Monad (when) import Data.Bits (Bits, shift, testBit, xor, (.&.), (.|.)) import System.Environment (getArgs) import System.Exit (exitFailure, exitSuccess) tap :: [[Int]] tap

Compile-time recursive function to compute the next power of two of an integer?

孤街醉人 提交于 2019-11-30 21:10:13
On the Bit Twiddling Hacks website the following algorithm is provided to round up an integer to the next power of two: unsigned int v; // compute the next highest power of 2 of 32-bit v v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; I would like to code a metaprogramming function that will compute the same operation: recursively (for compile-time execution) for any kind of integer (it should even work for possible awkward non-standard integers of any size like 15 bits, 65 bits...) and here is the form of the expected function: template <typename Type, // Something

Deriving nth Gray code from the (n-1)th Gray Code

泪湿孤枕 提交于 2019-11-30 21:07:00
问题 Is there a way to derive the 4-bit nth Gray code using the (n-1)th Gray code by using bit operations on the (n-1)th Gray Code? For example the 4th Gray code is 0010. Now I want to get the 5th Gray Code, 0110, by doing bit operations on 0010. 回答1: Perhaps it's "cheating" but you can just pack a lookup table into a 64-bit constant value, like this: 0000 0 -> 1 0001 1 -> 3 0011 3 -> 2 0010 2 -> 6 0110 6 -> 7 0111 7 -> 5 0101 5 -> 4 0100 4 -> C 1100 C -> D 1101 D -> F 1111 F -> E 1110 E -> A 1010

C++ - Bit-wise not of uchar produces int

心已入冬 提交于 2019-11-30 20:22:21
I am surprised by C++'s behavior when applying bit-wise not to an unsigned char. Take the binary value 01010101b , which is 0x55 , or 85 . Applying bit-wise not on an eight bit representation should yield 10101010b , which is 0xAA , or 170 . However, I cannot reproduce the above in C++. The following simple assertion fails. assert(static_cast<unsigned char>(0xAAu) == ~static_cast<unsigned char>(0x55u)); I printed the values of 0x55 , 0xAA , and ~0x55 (as uchar) with the following code. And it reveals that the bit-wise not does not do what I expect it to do. std::cout << "--> 0x55: " << 0x55u <

How can I combine 4 bytes into a 32 bit unsigned integer?

北慕城南 提交于 2019-11-30 20:06:00
I'm trying to convert 4 bytes into a 32 bit unsigned integer. I thought maybe something like: UInt32 combined = (UInt32)((map[i] << 32) | (map[i+1] << 24) | (map[i+2] << 16) | (map[i+3] << 8)); But this doesn't seem to be working. What am I missing? Your shifts are all off by 8. Shift by 24, 16, 8, and 0. Use the BitConverter class. Specifically, this overload. BitConverter.ToInt32() You can always do something like this: public static unsafe int ToInt32(byte[] value, int startIndex) { fixed (byte* numRef = &(value[startIndex])) { if ((startIndex % 4) == 0) { return *(((int*)numRef)); } if

Bitwise negation gives unexpected result

大憨熊 提交于 2019-11-30 20:03:26
I am trying to write a bitwise calculator in java, something that you could input an expression such as ~101 and it would give back 10 however when i run this code import java.util.Scanner; public class Test { public static void main(String[] args) { Integer a = Integer.valueOf("101", 2); System.out.println(Integer.toString(~a,2)); } } it outputs -110 why? You are assuming that 101 is three bits long. Java doesn't support variable length bit operations, it operates on a whole int of bits, so ~ will be the not of a 32 bit long "101". --- Edited after being asked "How can I fix this?" --- That's

How do computers find modulus?

非 Y 不嫁゛ 提交于 2019-11-30 19:40:24
Is there some cool algorithm with bit wise operations? Often, the modulus and divide operations on a processor are the same thing. For instance, refer to http://jsimlo.sk/docs/cpu/index.php/div.html . This is the implementation of the divide instruction on Intel processors. Most of the time, modulus is just computed by dividing the two numbers. The quotient is stored in one register, and the remainder is stored in the other register. You would go after the remainder. If the divisor is known in advance (e.g. for code produced by a C compiler, this is a constant known at compile time) then

Checking if bit is not set

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 19:24:43
If I use this: if(value & 4) to check if the bit is set, then how do I check if the bit isn't set? I tried with if(!value & 4) or if(~value & 4) and if(value ^ 4) but none of them works. When you write if(value & 4) , C checks the result to be non-zero. Essentially, it means if((value & 4) != 0) { ... } Therefore, if you would like to check that the bit is not set, compare the result for equality to zero: if((value & 4) == 0) { ... } You could do it many ways, but the easiest (easiest as in requires the least amount of thought) would be just negate the entire expression you already have: if (!