bit-shift

Bitshifting a long in Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 10:29:28
问题 Im sure this is an easy one for whoever sees it first! Why in Java does code like long one = 1 << 0; long thirty = 1 << 30; long thirtyOne = 1 << 31; long thirtyTwo = 1 << 32; System.out.println(one+" = "+Long.toBinaryString(1 << 0)); System.out.println(thirty+" = "+Long.toBinaryString(1 << 30)); System.out.println(thirtyOne+" = "+Long.toBinaryString(1 << 31)); System.out.println(thirtyTwo+" = "+Long.toBinaryString(1 << 32)); print 1 = 1 1073741824 = 1000000000000000000000000000000

Shifting a BitArray

本小妞迷上赌 提交于 2019-12-20 06:42:38
问题 I'm currently trying to shift a BitArray while keeping its length. Since there's no built-in method I'm struggling to build one but can't make it work, unfortunatly. My initial BitArray code sets a length of 421 for the BitArray. var b = new BitArray(length: 421); Than, I'm assigning some values for testing. For instance: b.Set(0, true); b.Set(1, true); However, I can't figure out how to shift the bit array. Attempts: - I thought that I could convert it into long and than make the bit

Zero fill right shift in python

怎甘沉沦 提交于 2019-12-20 05:42:40
问题 function(e, t) { return e << t | e >>> 32 - t } I have this method in js, I do not understand in deep about shift operation. I want to write that in python. How can I write the equivalent code in python as it does not support Zero Fill Right Shift Operator as in JS >>> . 回答1: There is not a built-in zero fill right shift operator in Python, but you can easily define your own zero_fill_right_shift function: def zero_fill_right_shift(val, n): return (val >> n) if val >= 0 else ((val +

How to convert a sequence of 32 char (0/1) to 32 bits (uint32_t)?

≡放荡痞女 提交于 2019-12-20 04:26:07
问题 I have an array of char (usually thousands of bytes long) read from a file, all composed of 0 and 1 (not '0' and '1', in which case I could use strtoul ). I want to pack these into single bits, thus converting each 32 char into a single uint32_t. Should I write a bit shift operation with 32 parts, or is there a saner way? out[i/32] = data[i] << 31 | data[i+1] << 30 | data[i+2] << 29 | data[i+3] << 28 | data[i+4] << 27 | data[i+5] << 26 | data[i+6] << 25 | data[i+7] << 24 | data[i+8] << 23 |

bit-shifting by an integer value

别来无恙 提交于 2019-12-20 03:30:42
问题 This code is for a cache simulator project - I am trying to extract certain bits from a memory address. When I attempt to use int variables to do the bit shifting, I end up with an incorrect result, but when I use the numbers directly, my result is correct. I've been looking all over for an answer to this, but I can't find one. What is my issue here? #include <stdio.h> void main(){ unsigned long long int mem_addr = 0x7fff5a8487c0; int byte_offset_bits = 2; int block_offset_bits = 5; int index

Confused by undefined C++ shift operator behavior and wrapping “pattern space”

早过忘川 提交于 2019-12-20 02:51:52
问题 I'm confused by something I read in the Shift Operators section of an article on undefined C++ behavior. On the ARM architecture, the shift operators always behave as if they take place in a 256-bit pattern space, regardless of the operand size--that is, the pattern repeats, or "wraps around", only every 256 positions. Another way of thinking of this is that the pattern is shifted the specified number of positions modulo 256. Then, of course, the result contains just the least-significant

Does bit shift automatically promote chars to int? [duplicate]

元气小坏坏 提交于 2019-12-20 02:39:07
问题 This question already has answers here : Bitshift and integer promotion? (2 answers) Closed 6 years ago . I read somewhere that bitwise shift automatically turns the operand into an int. But I'm not sure if that statement should be qualified with "if the operands are of unequal type." char one = 1, bitsInType = 8; one << (bitsInType - one); Does the default result of the second line result in an int or char? 回答1: The result type is int in normal C implementations. 1 Per C 2011 (N1570) 6.5.7,

undefined behavior when left operand is negative

南楼画角 提交于 2019-12-19 11:43:09
问题 few days back I gave Microsoft GD online exam for internship there. I have always studied that the left shift of negative number is an undefined behavior but that paper had almost 7 questions out of 30 related to shift operators and approx 5 questions were ther which involved shifting negative numbers to left and they had no option saying "undefined behavior". I was shocked to see that . So, my question is that has this C standard changed ? Is this defined now ? sample question : printf("%d",

How to implement arithmetic right shift from logical shift?

江枫思渺然 提交于 2019-12-19 11:28:36
问题 I need to implement a 32-bit arithmetic right shift from logical shifts, and, or, xor and normal integer arithmetic operations. I read somewhere the following is supposed to work: (x>>N)|(((1<<N)-1)<<(32-N)) x is the integer that will be shifted and N is the amount of bits to shift. This works for negative (msb is 1) numbers but not for positive numbers (msb is 0). Does anyone know an efficient algorithm that always produces the right result? 回答1: You can use this (x >> N) | (-(x < 0) << (32

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

我的梦境 提交于 2019-12-19 09:18:17
问题 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 回答1: /** * The >>> javascript operator in php x86