bit-shift

Why does combining two shifts of a uint8_t produce a different result?

人走茶凉 提交于 2019-11-27 09:48:50
Could someone explain me why: x = x << 1; x = x >> 1; and: x = (x << 1) >> 1; produce different answers in C? x is a *uint8_t* type (unsigned 1-byte long integer). For example when I pass it 128 (10000000) in the first case it returns 0 (as expected most significant bit falls out) but in the second case it returns the original 128 . Why is that? I'd expect these expressions to be equivalent? This is due to integer promotions, both operands of the bit-wise shifts will be promoted to int in both cases. In the second case: x = (x << 1) >> 1; the result of x << 1 will be an int and therefore the

What do two left-angle brackets “<<” mean in C#?

血红的双手。 提交于 2019-11-27 09:14:04
问题 Basically the questions in the title. I'm looking at the MVC 2 source code: [Flags] public enum HttpVerbs { Get = 1 << 0, Post = 1 << 1, Put = 1 << 2, Delete = 1 << 3, Head = 1 << 4 } and I'm just curious as to what the double left angle brackers << does. 回答1: That would be the bitwise left shift operator. For each shift left, the value is effectively multiplied by 2. So, for example, writing value << 3 will multiply the value by 8. What it really does internally is move all of the actual

Left bit shifting 255 (as a byte)

守給你的承諾、 提交于 2019-11-27 09:03:48
Can anyone explain why the following doesn't compile? byte b = 255 << 1 The error: Constant value '510' cannot be converted to a 'byte' I'm expecting the following in binary: 1111 1110 The type conversion has stumped me. Joey Numeric literals in C# are int , not byte (and the bit shift will be evaluated by the compiler, hence only the 510 remains). You are therefore trying to assign a value to a byte which does not fit. You can mask with 255: byte b = (255 << 1) & 0xFF to reduce the result to 8 bits again. Unlike Java, C# does not allow overflows to go by undetected. Basically you'd have two

Declaring 64-bit variables in C

社会主义新天地 提交于 2019-11-27 09:00:53
I have a question. uint64_t var = 1; // this is 000000...00001 right? And in my code this works: var ^ (1 << 43) But how does it know 1 should be in 64 bits? Shouldn’t I write this instead? var ^ ( (uint64_t) 1 << 43 ) As you supposed, 1 is a plain signed int (which probably on your platform is 32 bit wide in 2's complement arithmetic), and so is 43, so by any chance 1<<43 results in an overflow: in facts, if both arguments are of type int operator rules dictate that the result will be an int as well. Still, in C signed integer overflow is undefined behavior, so in line of principle anything

Bitwise shift operators. Signed and unsigned

血红的双手。 提交于 2019-11-27 08:03:58
I'm practising for the SCJP exam using cram notes from the Internet. According to my notes the >> operator is supposed to be signed right shift, with the sign bit being brought in from the left. While the left shift operator << is supposed to preserve the sign bit. Playing around however, I'm able to shift the sign with the << operator (f.e. Integer.MAX_VALUE << 1 evaluates to -2 , while I'm never able to shift the sign with the >> operator. I must be misunderstanding something here, but what? ">>" is signed because it keeps the sign. It uses the most left digit in binary representation of a

Bitwise shift operation on a 128-bit number

懵懂的女人 提交于 2019-11-27 07:07:01
问题 Lets say that I have an array of 4 32-bit integers which I use to store the 128-bit number How can I perform left and right shift on this 128-bit number? Thanks! 回答1: void shiftl128 ( unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d, size_t k) { assert (k <= 128); if (k >= 32) // shifting a 32-bit integer by more than 31 bits is "undefined" { a=b; b=c; c=d; d=0; shiftl128(a,b,c,d,k-32); } else { a = (a << k) | (b >> (32-k)); b = (b << k) | (c >> (32-k)); c = (c << k) | (d >>

Arithmetic right shift gives bogus result?

心不动则不痛 提交于 2019-11-27 04:45:23
I must be absolutely crazy here, but gcc 4.7.3 on my machine is giving the most absurd result. Here is the exact code that I'm testing: #include <iostream> using namespace std; int main(){ unsigned int b = 100000; cout << (b>>b) << endl; b = b >> b; cout << b << endl; b >>= b; cout << b << endl; return 0; } Now, any number that's right shifted by itself should result in 0 ( n/(2^n) == 0 with integer divide , n>1 , and positive/unsigned ), but somehow here is my output: 100000 100000 100000 Am I crazy? What could possibly be going on? In C++ as in C, shifts are limited to the size (in bits) of

Right shift and signed integer

余生长醉 提交于 2019-11-27 04:37:28
问题 On my compiler, the following pseudo code (values replaced with binary): sint32 word = (10000000 00000000 00000000 00000000); word >>= 16; produces a word with a bitfield that looks like this: (11111111 11111111 10000000 00000000) My question is, can I rely on this behaviour for all platforms and C++ compilers? 回答1: From the following link: INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand Noncompliant

Behaviour of unsigned right shift applied to byte variable

匆匆过客 提交于 2019-11-27 04:30:35
Consider the following snip of java code byte b=(byte) 0xf1; byte c=(byte)(b>>4); byte d=(byte) (b>>>4); output: c=0xff d=0xff expected output: c=0x0f how? as b in binary 1111 0001 after unsigned right shift 0000 1111 hence 0x0f but why is it 0xff how? The problem is that all arguments are first promoted to int before the shift operation takes place: byte b = (byte) 0xf1; b is signed, so its value is -15. byte c = (byte) (b >> 4); b is first sign-extended to the integer -15 = 0xfffffff1 , then shifted right to 0xffffffff and truncated to 0xff by the cast to byte . byte d = (byte) (b >>> 4); b

What does >> do in java?

时光毁灭记忆、已成空白 提交于 2019-11-27 04:25:43
Okay, I tried looking up what >>, or shift means, but it's way over my head as this site explains it: http://www.janeg.ca/scjp/oper/shift.html So can someone explain it like they're talking to a kid? Computers are binary devices. Because of this, numbers are represented by a sequence of 1s and 0s. Bitshifting is simply moving those sequences of 1s and 0s left or right. So all the >> operator does is shift the bits towards the right one bit. Consider the number 101: // Assuming signed 8-bit integers 01100101 // How 101 is represented in binary 00110010 // After right shifting one bit, this