bit-manipulation

What is the purpose of “int mask = ~0;”?

不想你离开。 提交于 2019-11-30 05:35:54
I saw the following line of code here in C. int mask = ~0; I have printed the value of mask in C and C++. It always prints -1 . So I do have some questions: Why assigning value ~0 to the mask variable? What is the purpose of ~0 ? Can we use -1 instead of ~0 ? It's a portable way to set all the binary bits in an integer to 1 bits without having to know how many bits are in the integer on the current architecture. C and C++ allow 3 different signed integer formats: sign-magnitude, one's complement and two's complement ~0 will produce all-one bits regardless of the sign format the system uses. So

Sign extension with bitwise shift operation

倾然丶 夕夏残阳落幕 提交于 2019-11-30 05:28:10
问题 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

Dividing by power of 2 using bit shifting

倾然丶 夕夏残阳落幕 提交于 2019-11-30 05:22:19
I've got the following task: Compute x/(2^n) , for 0 <= n <= 30 using bit shifting. Requirement: Round toward zero. Examples: divpwr2(15,1) = 7 divpwr2(-33,4) = -2 Legal operators: ! ~ & ^ | + << >> Maximum number of operators: 15 Here is what I've got so far: public int DivideByPowerOf2(int x, int n) { //TODO: find out why DivideByPowerOf2(-33,4) = -3 instead of -2 return x >> n; } DivideByPowerOf2(15,1) = 7 is ok. But DivideByPowerOf2(-33,4) = -3 instead of -2. Why? Kurt Price After looking for a good answer myself, I stumbled across this and was able to get a working snippet. Let me help

How does this code work to reverse bits in number?

只愿长相守 提交于 2019-11-30 05:18:45
unsigned reverse_bits(unsigned input) { //works on 32-bit machine input = (input & 0x55555555) << 1 | (input & 0xAAAAAAAA) >> 1; input = (input & 0x33333333) << 2 | (input & 0xCCCCCCCC) >> 2; input = (input & 0x0F0F0F0F) << 4 | (input & 0xF0F0F0F0) >> 4; input = (input & 0x00FF00FF) << 8 | (input & 0xFF00FF00) >> 8; input = (input & 0x0000FFFF) << 16 | (input & 0xFFFF0000) >> 16; return input; } how does this work? Suppose I have a hand of 8 cards: 7 8 9 10 J Q K A How can we reverse them? One way is to swap adjacent pairs: 8 7 10 9 Q J A K Then, swap adjacent groups of 2: exchange 8 7 and 10

Efficient bit-fiddling in a LFSR implementation

我是研究僧i 提交于 2019-11-30 05:17:36
问题 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, (.&.), (.|.)

How can I shuffle bits efficiently?

故事扮演 提交于 2019-11-30 05:01:19
I need to shuffle a 16 bit unsigned integer in a way that the even indexes land in the lower byte, and the odd indexes land in the upper byte. input: fedcba98 76543210 (contiguously numbered) output: fdb97531 eca86420 (even and odd separated) My code looks like this at the moment: typedef unsigned short u16; u16 segregate(u16 x) { u16 g = (x & 0x0001); u16 h = (x & 0x0004) >> 1; u16 i = (x & 0x0010) >> 2; u16 j = (x & 0x0040) >> 3; u16 k = (x & 0x0100) >> 4; u16 l = (x & 0x0400) >> 5; u16 m = (x & 0x1000) >> 6; u16 n = (x & 0x4000) >> 7; u16 o = (x & 0x0002) << 7; u16 p = (x & 0x0008) << 6;

Bit Twiddling Hacks: interleave bits the obvious way [closed]

橙三吉。 提交于 2019-11-30 04:57:11
i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html ) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; // bits of x are in the even positions and y in the odd; unsigned int z = 0; // z gets the resulting Morton Number. for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) // unroll for more speed... { z |= (x & 1U << i) << i | (y & 1U << i) << (i + 1); } can someone explain to me how this works with an example? for example if we have x = 100101 and y = 010101 , what will be result?

Bitwise negation gives unexpected result

人盡茶涼 提交于 2019-11-30 04:37:46
问题 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? 回答1: 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

Bitwise operation - Zero-fill right shift (>>>) usages?

谁都会走 提交于 2019-11-30 04:15:19
问题 Generally speaking, bit shifting ( >> , << ) allows us to divide / multiply by ^2 Example : 9 (base 10): 00000000000000000000000000001001 (base 2) -------------------------------- 9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10) For negative numbers : Likewise, -9 >> 2 yields -3 , because the sign is preserved: -9 (base 10): 11111111111111111111111111110111 (base 2) -------------------------------- -9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3

Can XOR of two integers go out of bounds?

这一生的挚爱 提交于 2019-11-30 04:10:44
I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The result is 5 . My question is - supposedly the integers (getting generated by the XOR operation) are too large due to this operation: LonelyInteger ^ arr[i] Which leads to a potentially large integer which cannot be represented by the datatype say int in this case. My questions are: Is it even possible that XOR will generate such a large integer value that