bit-shift

bit shifting with unsigned long type produces wrong results

倾然丶 夕夏残阳落幕 提交于 2019-11-26 04:52:07
问题 I\'m a bit confused because I wanted to initialize a variable of type unsigned long whose size is 8 bytes on my system (on every modern system I suppose). When I want to assign 1 << 63 to the variable, I get a compiler warning however and the number is in fact 0. When I do 1 << 30 I get the expected result of 2 ^ 30 = 1073741824 . Yet when I do 1 << 31 , I get the result of 2 ^ 64 (I think; actually this shouldn\'t be possible) which prints 18446744071562067968 . Can anyone explain this

Weird behavior of right shift operator (1 >> 32)

血红的双手。 提交于 2019-11-26 04:27:23
问题 I recently faced a strange behavior using the right-shift operator. The following program: #include <cstdio> #include <cstdlib> #include <iostream> #include <stdint.h> int foo(int a, int b) { return a >> b; } int bar(uint64_t a, int b) { return a >> b; } int main(int argc, char** argv) { std::cout << \"foo(1, 32): \" << foo(1, 32) << std::endl; std::cout << \"bar(1, 32): \" << bar(1, 32) << std::endl; std::cout << \"1 >> 32: \" << (1 >> 32) << std::endl; //warning here std::cout << \"(int)1 >

Bitwise operators and “endianness”

跟風遠走 提交于 2019-11-26 03:09:52
问题 Does endianness matter at all with the bitwise operations? Either logical or shifting? I\'m working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I\'m getting quite hung up on the endianess. That is, I\'m using a little endian machine (like most are), but does this need to be considered or is it a wasted fact? In case it matters, I\'m using C. 回答1: Endianness only matters for layout of data in memory. As soon as data is loaded by the

What does the C standard say about bitshifting more bits than the width of type?

橙三吉。 提交于 2019-11-26 02:59:07
问题 Consider the following code: int i = 3 << 65; I would expect that the result is i==0 , however the actual result is i==6 . With some testing I found that with the following code: int i, s; int a = i << s; int b = i << (s & 31); the values of a and b are always the same. Does the C standard say anything about shifting more than 32 bits (the width of type int ) or is this unspecified behavior? 回答1: From my WG12/N1124 draft ( not the standard, but Good Enough For Me), there's the following block

What is the JavaScript >>> operator and how do you use it?

只谈情不闲聊 提交于 2019-11-26 02:39:09
问题 I was looking at code from Mozilla that add a filter method to Array and it had a line of code that confused me. var len = this.length >>> 0; I have never seen >>> used in JavaScript before. What is it and what does it do? 回答1: It doesn't just convert non-Numbers to Number, it converts them to Numbers that can be expressed as 32-bit unsigned ints. Although JavaScript's Numbers are double-precision floats(*), the bitwise operators ( << , >> , & , | and ~ ) are defined in terms of operations on

How do shift operators work in Java? [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 02:28:07
问题 This question already has answers here : What are bitwise shift (bit-shift) operators and how do they work? (9 answers) Closed last year . I am trying to understand the shift operators and couldn\'t get much. When I tried to execute the below code System.out.println(Integer.toBinaryString(2 << 11)); System.out.println(Integer.toBinaryString(2 << 22)); System.out.println(Integer.toBinaryString(2 << 33)); System.out.println(Integer.toBinaryString(2 << 44)); System.out.println(Integer

Is multiplication and division using shift operators in C actually faster?

此生再无相见时 提交于 2019-11-26 02:27:27
问题 Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to use say (i<<3)+(i<<1) to multiply with 10 than using i*10 directly? Is there any sort of input that can\'t be multiplied or divided in this way? 回答1: Short answer: Not likely. Long answer: Your compiler has an optimizer in it that knows how to multiply as quickly as your target processor architecture is capable. Your best bet is

What are bitwise shift (bit-shift) operators and how do they work?

萝らか妹 提交于 2019-11-26 01:18:17
问题 I\'ve been attempting to learn C in my spare time, and other languages (C#, Java, etc.) have the same concept (and often the same operators) ... What I\'m wondering is, at a core level, what does bit-shifting ( << , >> , >>> ) do, what problems can it help solve, and what gotchas lurk around the bend? In other words, an absolute beginner\'s guide to bit shifting in all its goodness. 回答1: The bit shifting operators do exactly what their name implies. They shift bits. Here's a brief (or not-so

Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?

萝らか妹 提交于 2019-11-26 01:01:58
问题 In C bitwise left shift operation invokes Undefined Behaviour when the left side operand has negative value. Relevant quote from ISO C99 (6.5.7/4) The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2 E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that

Right shifting negative numbers in C

懵懂的女人 提交于 2019-11-26 00:55:35
问题 I have C code in which I do the following. int nPosVal = +0xFFFF; // + Added for ease of understanding int nNegVal = -0xFFFF; // - Added for valid reason Now when I try printf (\"%d %d\", nPosVal >> 1, nNegVal >> 1); I get 32767 -32768 Is this expected? I am able to think something like 65535 >> 1 = (int) 32767.5 = 32767 -65535 >> 1 = (int) -32767.5 = -32768 That is, -32767.5 is rounded off to -32768. Is this understanding correct? 回答1: It looks like your implementation is probably doing an