bit-shift

Understanding bitwise operations and their application in Java

一曲冷凌霜 提交于 2019-12-18 07:12:40
问题 I think understand what they fundamentally do - operate on bits (flip, shift, invert, etc...). My issue is that I don't know when I'd ever need to use them, and I don't think I fully understand bits. I know that there are 8 bits in a byte and I know that bits are either a 0 or 1 . Now here is where I start to get confused... I believe data types define combinations of bits differently. So if I declare an int , 32 bits are set aside for numbers, if I declare a char, 8 bits are set aside and

Arithmetic shift acts as a logical shift, regardless of the signed variable

老子叫甜甜 提交于 2019-12-18 06:14:28
问题 I've got a register declared as so: logic signed [15:0][2:0][15:0] registers; When I place a 2's compliment number into the array and arithmetically shift the number, it logical shifts instead: registers[0][0] = 16'b1000000000000000; registers[0][0] = registers[0][0]>>>2; Apparently, the system will logical shift instead of arithmetically shift if the number is not signed. However as you can clearly see, 'registers' is definitely signed. Does anybody know what I might be missing here? Thanks!

Arithmetic shift acts as a logical shift, regardless of the signed variable

断了今生、忘了曾经 提交于 2019-12-18 06:14:10
问题 I've got a register declared as so: logic signed [15:0][2:0][15:0] registers; When I place a 2's compliment number into the array and arithmetically shift the number, it logical shifts instead: registers[0][0] = 16'b1000000000000000; registers[0][0] = registers[0][0]>>>2; Apparently, the system will logical shift instead of arithmetically shift if the number is not signed. However as you can clearly see, 'registers' is definitely signed. Does anybody know what I might be missing here? Thanks!

Shifting the sign bit in .NET

二次信任 提交于 2019-12-18 05:43:13
问题 I'm reading bits from a monochrome bitmap. I'm storing every 16 bits in a short in the reverse order. If the bit in the bitmap is black, store a 1. If white, store a 0. E.g.: for bitmap: bbbw bbbw bbbw wwww my short is: 0000 0111 0111 0111 The 1st way I tried to do this was: short m; // ... Color c = bmp.GetPixel(j, i); if (c.R == Color.Black) m |= short.MinValue; m >>= 1; // ... After one assignment and shift, I got the expected -32768 (1000 0000 0000 0000). After the 2nd time I got -16384

Negative logical shift

空扰寡人 提交于 2019-12-18 03:46:42
问题 In Java, why does -32 >>> -1 = 1 ? It's not specific to just -32. It works for all negative numbers as long as they're not too big. I've found that x >>> -1 = 1 x >>> -2 = 3 x >>> -3 = 7 x >>> -4 = 15 given 0 > x > some large negative number Isn't >>> -1 the same as << 1? But -32 << 1 = -64. I've read up on two's complements, but still don't understand the reasoning. 回答1: It's because when you are shifting a 32-bit int , it just takes the last 5 bits of the shift distance. (i.e. mod 32), so

What is the best practice way to create a bitmask for a range of bits?

£可爱£侵袭症+ 提交于 2019-12-17 20:55:59
问题 I can think of three ways to do this off the top of my head. I'll outline them real quick. char mask = (1<<top) mask = mask-1 mask = mask>>bot mask = mask<<bot 3 shifts, 1 addition char topMask = (1<<top) topMask = topMask -1 char botMask = (1<<bot) botMask = botMask - 1 char mask = topMask - botMask 2 shifts, 3 additions char mask = (1<<(top-bot)) mask = mask - 1 mask = mask << bot 2 shifts, 2 additions It seems like the first one would be a little faster? Is one considered best for style

What happens if we bitwise shift an integer more than its size [duplicate]

為{幸葍}努か 提交于 2019-12-17 20:53:15
问题 This question already has answers here : Unexpected C/C++ bitwise shift operators outcome (6 answers) Closed 6 years ago . On Visual Studio compiling following C code , the result is 4 . void main() { int c = 1; c = c<<34;} The assembly code as seen from on Visual Studio disassembly window is shl eax,22h From assembly , its easy to see that we are shifting 34. Since the integer here is 4 bytes , from the results it is obvious that modulo operation was carried out on machine level to make this

Weird result after assigning 2^31 to a signed and unsigned 32-bit integer variable

ε祈祈猫儿з 提交于 2019-12-17 20:49:07
问题 As the question title reads, assigning 2^31 to a signed and unsigned 32-bit integer variable gives an unexpected result. Here is the short program (in C++ ), which I made to see what's going on: #include <cstdio> using namespace std; int main() { unsigned long long n = 1<<31; long long n2 = 1<<31; // this works as expected printf("%llu\n",n); printf("%lld\n",n2); printf("size of ULL: %d, size of LL: %d\n", sizeof(unsigned long long), sizeof(long long) ); return 0; } Here's the output: MyPC /

Is java bit shifting circular?

耗尽温柔 提交于 2019-12-17 20:37:19
问题 I have this behavior using Java: int b=16; System.out.println(b<<30); System.out.println(b<<31); System.out.println(b<<32); System.out.println(b<<33); output: 0 0 16 32 Is java bit shift circular? IF not, why I get 0 when b<<30 and 16 when b<<32? 回答1: Bit shifting is not circular; for bit-shifting int s, Java only uses the 5 least-significant bits, so that (b << 0) is equivalent to (b << 32) (is equivalent to (b << 64) , etc.). You can simply take the bit-shifting amount and take the

Are bitwise rotations slower than shifts on current Intel CPU?

一笑奈何 提交于 2019-12-17 20:28:10
问题 I was curious if java.lang.Integer.rotateLeft gets optimized by using a rotation instruction and wrote a benchmark for it. The results were inconclusive: It was much faster than two shifts but a bit slower than a single one. So I rewrote it in C++ and got about the same results. When compiling via g++ -S -Wall -O3 I can see the instruction in the generated assembler. My CPU is Intel Core i5. The benchmark is quite long and surely not the nicest piece of code, but I don't think it's broken. Or