bit-shift

How to extract specific bits from a number in C?

你。 提交于 2019-11-28 18:21:57
I need to extract specific part (no of bits) of a short data type in C. For Example I have a binary of 52504 as 11001101000 11000 and I want First 6 ( FROM LSB --> MSB i.e 011000 decimal 24) bits and rest of 10 bits ( 11001101000 decimal 820). Similarly I want this function to be too generalized to extract specific no of bits given "start" and "end" (i.e chunks of bits equivalent with some decimal value). I checked other posts, but those were not helpful, as given functions are not too much generalized. I need something that can work for short data type of C. Edit I am having the short array

What does AND 0xFF do?

我只是一个虾纸丫 提交于 2019-11-28 15:46:35
问题 In the following code: short = ((byte2 << 8) | (byte1 & 0xFF)) What is the purpose of &0xFF ? Because other somestimes I see it written as: short = ((byte2 << 8) | byte1) And that seems to work fine too? 回答1: Anding an integer with 0xFF leaves only the least significant byte. For example, to get the first byte in a short s , you can write s & 0xFF . This is typically referred to as "masking". If byte1 is either a single byte type (like uint8_t ) or is already less than 256 (and as a result is

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

北战南征 提交于 2019-11-28 15:28:35
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. 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 bits of the value left one place. So if you have the value 12 (decimal), in binary that is 00001100 ; shifting

Times-two faster than bit-shift, for Python 3.x integers?

℡╲_俬逩灬. 提交于 2019-11-28 15:25:53
问题 I was looking at the source of sorted_containers and was surprised to see this line: self._load, self._twice, self._half = load, load * 2, load >> 1 Here load is an integer. Why use bit shift in one place, and multiplication in another? It seems reasonable that bit shifting may be faster than integral division by 2, but why not replace the multiplication by a shift as well? I benchmarked the the following cases: (times, divide) (shift, shift) (times, shift) (shift, divide) and found that #3

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

前提是你 提交于 2019-11-28 14:01:45
This question already has an answer here: Unexpected C/C++ bitwise shift operators outcome 6 answers 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 work as shift by 2. I was wondering if this behavior is standardized across platforms or varies across platforms? Undefined

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

非 Y 不嫁゛ 提交于 2019-11-28 13:46:44
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 / # c++ test.cpp -o test MyPC / # ./test 18446744071562067968 <- Should be 2^31 right? -2147483648 <-

Bitwise shift operation on a 128-bit number

为君一笑 提交于 2019-11-28 12:36:56
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! Remus Rusanu 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 >> (32-k)); d = (d << k); } } void shiftr128 ( unsigned int& a, unsigned int& b, unsigned int& c,

Is java bit shifting circular?

烂漫一生 提交于 2019-11-28 12:26:49
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? 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 remainder when dividing by 32. Something similar occurs for bit-shifting long s, where Java only uses the 6 least

What do the << and >> operator do? [duplicate]

こ雲淡風輕ζ 提交于 2019-11-28 11:40:39
问题 This question already has an answer here: What do these JavaScript bitwise operators do? 3 answers I came across some code as noted below and am confused as to what it's doing. hash += (hash << 10); 回答1: It's a Bitwise Operator. Here's an example from the MDN (linked to above): 9 (base 10): 00000000000000000000000000001001 (base 2) -------------------------------- 9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10) See how the 1s have shifted? 回答2: This is one of the

Is unsigned long int correct for this operation?

天涯浪子 提交于 2019-11-28 11:38:48
问题 Here's my code: #include <stdio.h> int main(int argc, char *argv[]) { unsigned long int x = 0; // trying to make x = 2,147,483,648 x = 1 << 31; printf("%lu", x); } It's returning that x = 18446744071562067968. I read that unsigned long int should go up to 4,294,967,296, so why can't I use 1 << 32 to set x equal to 2,147,483,648? 回答1: 1 << 31 causes undefined behaviour, if your system has 32-bit ints. The literal 1 is a signed int. You need to do an unsigned shift instead of a signed shift: x