bit-shift

Masking bits within a range given in parameter in C

坚强是说给别人听的谎言 提交于 2019-11-26 23:40:01
问题 I am new to C programming and not sure that there is already a good explanation for how to do this, if so I am sorry. I am trying to set the bits within a range given to me. the function signature looks like: unsigned int setBits(int low, int high, unsigned int source) { source being the number to be operated on, low being the lowest bit in the range, and high being the highest bit in the range. I understand bit-shifting just fine when trying to get specifically the last 4 bits or first 4 or

Bitshift in javascript

南楼画角 提交于 2019-11-26 23:26:40
问题 I've got a really big number: 5799218898. And want to shift it right to 13 bits. So, windows-calculator or python gives me: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 70791 | 10001010010000111 As expected. But Javascript: 5799218898 >> 13 | 100010100100001110011111100001 >> 13 183624 | 101100110101001000 I think it because of internal integer representation in javascript, but cannot find anything about that. 回答1: In ECMAScript (Javascript) bitwise operations are always in 32-bit.

Shifting a 32 bit integer by 32 bits

ⅰ亾dé卋堺 提交于 2019-11-26 23:17:47
问题 I'm slinging some C code and I need to bitshift a 32 bit int left 32 bits. When I run this code with the parameter n = 0, the shifting doesn't happen. int x = 0xFFFFFFFF; int y = x << (32 - n); Why doesn't this work? 回答1: Shift at your own peril. Per the standard, what you want to do is undefined behavior . C99 §6.5.7 3 - The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or

Why doesn't left bit shift << shift beyond 31 for long int datatype?

痴心易碎 提交于 2019-11-26 23:17:27
问题 I want to use the following code in my program but gcc won't allow me to left shift my 1 beyond 31. sizeof(long int) displays 8, so doesn't that mean I can left shift till 63? #include <iostream> using namespace std; int main(){ long int x; x=(~0 & ~(1<<63)); cout<<x<<endl; return 0; } The compiling outputs the following warning: left shift `count >= width` of type [enabled by default] `x=(~0 & ~(1<<63))`; ^ and the output is -1. Had I left shifted 31 bits I get 2147483647 as expected of int.

Does Go compiler's evaluation differ for constant expression and other expression

雨燕双飞 提交于 2019-11-26 23:06:50
Why does below code fail to compile? package main import ( "fmt" "unsafe" ) var x int = 1 const ( ONE int = 1 MIN_INT int = ONE << (unsafe.Sizeof(x)*8 - 1) ) func main() { fmt.Println(MIN_INT) } I get an error main.go:12: constant 2147483648 overflows int Above statement is correct. Yes, 2147483648 overflows int (In 32 bit architecture). But the shift operation should result in a negative value ie -2147483648. But the same code works, If I change the constants into variables and I get the expected output. package main import ( "fmt" "unsafe" ) var x int = 1 var ( ONE int = 1 MIN_INT int = ONE

Is left and right shifting negative integers defined behavior?

蓝咒 提交于 2019-11-26 22:52:25
I know, right shifting a negative signed type depends on the implementation, but what if I perform a left shift? For example: int i = -1; i << 1; Is this well-defined? I think the standard doesn't say about negative value with signed type if E1 has a signed type and non-negative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined. It only clarifies that if the result isn't representable in signed type then the behavior is undefined. You're not reading that sentence correctly. The standard defines it if: the left

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

匆匆过客 提交于 2019-11-26 22:25:27
问题 1). var bitValue = (byteValue & (1 << bitNumber)) != 0; 2). using System.Collections.BitArray with a Get(int index) method What is faster? In what situations for the .NET projects BitArray could be more useful than a simple conjunction with the bitwise shift? 回答1: BitArray is going to be able to handle an arbitrary number of boolean values, whereas a byte will hold only 8, int only 32, etc. This is going to be the biggest difference between the two. Also, BitArray implements IEnumerable ,

C left shift on 64 bits fail

一曲冷凌霜 提交于 2019-11-26 22:04:48
问题 I have this code in C (it's for study only): char x; uint64_t total = 0; for(x = 20; x < 30; x++){ total = (((((1 << x) * x) / 64) + 1) * sizeof(uint64_t)); printf("%d - %llu\n", x, total); } What is printed: 20 - 2621448 21 - 5505032 22 - 11534344 23 - 24117256 24 - 50331656 25 - 104857608 26 - 218103816 27 - 18446744073625665544 28 - 18446744073575333896 29 - 18446744073508225032 Why at x > 26 do I have those strange values? I'm at gcc 4.6.1 on Ubuntu 10.10 64 bits. 回答1: Because 1 is an int

c get nth byte of integer

試著忘記壹切 提交于 2019-11-26 21:56:46
I know you can get the first byte by using int x = number & ((1<<8)-1); or int x = number & 0xFF; But I don't know how to get the nth byte of an integer. For example, 1234 is 00000000 00000000 00000100 11010010 as 32bit integer How can I get all of those bytes? first one would be 210, second would be 4 and the last two would be 0. int x = (number >> (8*n)) & 0xff; where n is 0 for the first byte, 1 for the second byte, etc. For the (n+1)th byte in whatever order they appear in memory (which is also least- to most- significant on little-endian machines like x86): int x = ((unsigned char *)(

Why does (1 << 31) >> 31 result in -1? [duplicate]

眉间皱痕 提交于 2019-11-26 21:55:37
问题 This question already has answers here : why shift int a=1 to left 31 bits then to right 31 bits, it becomes -1 (4 answers) Closed 2 years ago . int z = 1; z <<= 31; z >>= 31; printf ("%d\n",z); When I run the code, z=-1 , why? 回答1: int z = 1; z <<= 31; Assuming int is 32 bit and two's complement representation is used, the left shift is undefined behavior in C because the result if not representable in the int type. From the standard: The result of E1 << E2 is E1 left-shifted E2 bit