bit-shift

Signed left shift behaviour

喜欢而已 提交于 2019-12-01 13:48:31
public class Shift { public static void main(String[] args) { for(int i = 0; i < 32; ++i){ System.out.println(-0x55555555 << i); } } } Running the above code gives the following output -1431655765 1431655766 -1431655764 1431655768 -1431655760 1431655776 -1431655744 1431655808 -1431655680 1431655936 -1431655424 1431656448 -1431654400 1431658496 -1431650304 1431666688 -1431633920 1431699456 -1431568384 1431830528 -1431306240 1432354816 -1430257664 1434451968 -1426063360 1442840576 -1409286144 1476395008 -1342177280 1610612736 -1073741824 -2147483648 While testing with an other value (64) gives a

Signed left shift behaviour

旧城冷巷雨未停 提交于 2019-12-01 12:40:19
问题 public class Shift { public static void main(String[] args) { for(int i = 0; i < 32; ++i){ System.out.println(-0x55555555 << i); } } } Running the above code gives the following output -1431655765 1431655766 -1431655764 1431655768 -1431655760 1431655776 -1431655744 1431655808 -1431655680 1431655936 -1431655424 1431656448 -1431654400 1431658496 -1431650304 1431666688 -1431633920 1431699456 -1431568384 1431830528 -1431306240 1432354816 -1430257664 1434451968 -1426063360 1442840576 -1409286144

RGB histogram using bitshift in matlab

試著忘記壹切 提交于 2019-12-01 11:10:26
问题 I'm trying to create a mozaic image in Matlab. The database consists of mostly RGB images but also some gray scale images. I need to calculate the histograms - like in the example of the Wikipedia article about color histograms - for the RGB images and thought about using the bitshift operator in Matlab to combine the R,G and B channels. nbins = 4; nbits = 8; index = bitshift(bitshift(image(:,:,1), log2(nbins)-nbits), 2*log2(nbins)) + ... + bitshift(bitshift(image(:,:,2), log2(nbins)-nbits),

Bitwise Leftshift (<<) strange behavior

前提是你 提交于 2019-12-01 08:52:06
问题 gcc bitwise Leftshift ( << ) strange behavior. Here is my code: #include <stdio.h> #include <string.h> void foo(int n){ printf("1<<32:%d\n", 1<<32); printf("1<<(32-n):%d\n", 1<<(32-n)); } int main(){ foo(0); } If I pass 0 as parameter, the result could be different. Compiling the source code: $gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1 main.c: In function 'foo': main.c:5:3: warning: left shift count >= width of type [enabled by default] Executing the program: $demo 1<<32:0 1<<(32-n

C# Left Shift Operator

耗尽温柔 提交于 2019-12-01 06:31:16
There's a statement a co-worker of mine wrote which I don't completely understand. Unfortunately he's not available right now, so here it is (with modified names, we're working on a game in Unity). private readonly int FRUIT_LAYERS = (1 << LayerMask.NameToLayer("Apple")) | (1 << LayerMask.NameToLayer("Banana")); NameToLayer takes a string and returns an integer. I've always seen left shift operators used with the constant integer on the right side, not the left, and all the examples I'm finding via Google follow that approach. In this case, I think he's pushing Apple and Banana onto the same

sra(shift right arithmetic) vs srl (shift right logical)

只谈情不闲聊 提交于 2019-12-01 06:26:42
Please take a look at these two pieces of pseudo-assembly code: 1) li $t0,53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 2) li $t0,-53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 in the first case the output is: 212 13 13 in the latter is: -212 107374... -14 But shouldn't : sra (-53) = - (srl 53) ? -53 = 1111111111001011 sra 2 1111111111110010(11) = -14 ^^ ^^ sign dropped extension Because the extra bits are simply dropped for both positive and negative results, the result is always rounded down if you view the shift as a division. 53 sra

PHP equivalent javascript >>> shift right with zero fill bitwise operators?

不想你离开。 提交于 2019-12-01 06:20:49
May I know how can I do PHP >>> ? Such operators is not available in PHP, but is available in Javascript. I just managed to discover a function as follow: function zeroFill($a, $b) { $z = hexdec(80000000); if ($z & $a) { $a = ($a>>1); $a &= (~$z); $a |= 0x40000000; $a = ($a>>($b-1)); } else { $a = ($a>>$b); } return $a; } but unfortunately, it doesn't work perfectly. EG: -1149025787 >>> 0 Javascript returns 3145941509 PHP zeroFill() return 0 /** * The >>> javascript operator in php x86_64 * Usage: -1149025787 >>> 0 ---> rrr(-1149025787, 0) === 3145941509 * @param int $v * @param int $n *

C# Left Shift Operator

為{幸葍}努か 提交于 2019-12-01 05:37:30
问题 There's a statement a co-worker of mine wrote which I don't completely understand. Unfortunately he's not available right now, so here it is (with modified names, we're working on a game in Unity). private readonly int FRUIT_LAYERS = (1 << LayerMask.NameToLayer("Apple")) | (1 << LayerMask.NameToLayer("Banana")); NameToLayer takes a string and returns an integer. I've always seen left shift operators used with the constant integer on the right side, not the left, and all the examples I'm

LC3 Assembly Bitwise Right Shift

╄→гoц情女王★ 提交于 2019-12-01 04:31:43
What I need to do it implement both a bitwise left shift, and a bitwise right shift using LC-3 Assembly . Basically, every bit has to be moved over one space in the direction of the shift, and a zero fills the empty space created. Examples: Right Shift: 01001001 00100100→ Left Shift: 01001001 ←10010010 I've successfully implemented a left shift, by taking the binary string, and adding it to itself. I'm stumped on how to perform a right shift. Any thoughts would be greatly appreciated. I have AND, NOT, ADD operations, data movement operations, seven registers to store values and whole range of

How are shifts implemented on the hardware level?

耗尽温柔 提交于 2019-12-01 02:56:48
How are bit shifts implemented at the hardware level when the number to shift by is unknown? I can't imagine that there would be a separate circuit for each number you can shift by (that would 64 shift circuits on a 64-bit machine), nor can I imagine that it would be a loop of shifts by one (that would take up to 64 shift cycles on a 64-bit machine). Is it some sort of compromise between the two or is there some clever trick? The circuit is called a " barrel shifter " - it's a load of multiplexers basically. It has a layer per address-bit-of-shift-required, so an 8-bit barrel shifter needs