bit-manipulation

C++ signed and unsigned int vs long long speed

隐身守侯 提交于 2021-02-08 13:42:29
问题 Today, I noticed that the speed of several simple bitwise and arithmetic operations differs significantly between int , unsigned , long long and unsigned long long on my 64-bit pc. In particular, the following loop is about twice as fast for unsigned as for long long , which I didn't expect. int k = 15; int N = 30; int mask = (1 << k) - 1; while (!(mask & 1 << N)) { int lo = mask & ~(mask - 1); int lz = (mask + lo) & ~mask; mask |= lz; mask &= ~(lz - 1); mask |= (lz / lo / 2) - 1; } (full

C++ signed and unsigned int vs long long speed

北战南征 提交于 2021-02-08 13:42:06
问题 Today, I noticed that the speed of several simple bitwise and arithmetic operations differs significantly between int , unsigned , long long and unsigned long long on my 64-bit pc. In particular, the following loop is about twice as fast for unsigned as for long long , which I didn't expect. int k = 15; int N = 30; int mask = (1 << k) - 1; while (!(mask & 1 << N)) { int lo = mask & ~(mask - 1); int lz = (mask + lo) & ~mask; mask |= lz; mask &= ~(lz - 1); mask |= (lz / lo / 2) - 1; } (full

which is faster to find the even number if(n%2==0) or if(n&1==0)? [duplicate]

天涯浪子 提交于 2021-02-08 12:14:05
问题 This question already has answers here : Is & faster than % when checking for odd numbers? (8 answers) Closed 2 years ago . which is faster to find the even number if(n%2==0) or if(n&1==0) ? def isEven(n): if n%2 == 0: return True return False OR def isEven(n): if n&1 == 1: return False return True 回答1: This looks like Python, so I tried it in Python(3.6): from timeit import timeit a = timeit(stmt='for i in range(100): i%2') b = timeit(stmt='for i in range(100): i&1') print(a, b) Times vary

which is faster to find the even number if(n%2==0) or if(n&1==0)? [duplicate]

戏子无情 提交于 2021-02-08 12:11:36
问题 This question already has answers here : Is & faster than % when checking for odd numbers? (8 answers) Closed 2 years ago . which is faster to find the even number if(n%2==0) or if(n&1==0) ? def isEven(n): if n%2 == 0: return True return False OR def isEven(n): if n&1 == 1: return False return True 回答1: This looks like Python, so I tried it in Python(3.6): from timeit import timeit a = timeit(stmt='for i in range(100): i%2') b = timeit(stmt='for i in range(100): i&1') print(a, b) Times vary

which is faster to find the even number if(n%2==0) or if(n&1==0)? [duplicate]

孤者浪人 提交于 2021-02-08 12:10:22
问题 This question already has answers here : Is & faster than % when checking for odd numbers? (8 answers) Closed 2 years ago . which is faster to find the even number if(n%2==0) or if(n&1==0) ? def isEven(n): if n%2 == 0: return True return False OR def isEven(n): if n&1 == 1: return False return True 回答1: This looks like Python, so I tried it in Python(3.6): from timeit import timeit a = timeit(stmt='for i in range(100): i%2') b = timeit(stmt='for i in range(100): i&1') print(a, b) Times vary

extract bits from 32 bit float numbers in C

筅森魡賤 提交于 2021-02-08 10:36:30
问题 32 bits are represented in binary using the IEEE format. So how can I extract those bits? Bitwise operations like & and | do not work on them! what i basically want to do is extract the LSB from 32 bit float images in opencv thanx in advance! 回答1: uint32_t get_float_bits(float f) { assert(sizeof(float) == sizeof(uint32_t)); // or static assert uint32_t bits; memcpy(&bits, &f, sizeof f); return bits; } As of C99, the standard guarantees that the union trick works (provided the sizes match),

How to use ArrayBuffers with DataViews in JavaScript

此生再无相见时 提交于 2021-02-08 07:51:50
问题 The only real tutorial I have seen for ArrayBuffer is from HTML5Rocks. But I am wondering specifically how to manipulate the individual bytes. For example, this cartoon on ArrayBuffers from Mozilla shows an image of an ArrayBuffer wrapped in a Uint8Array view: It gives the feeling that you can do this with an ArrayBuffer: var x = new ArrayBuffer(10) x[0] = 1 x[1] = 0 ... x[9] = 1 That is, manually setting the bytes. But I haven't seen any documentation on such a feature. Instead, it seems you

How to use ArrayBuffers with DataViews in JavaScript

旧时模样 提交于 2021-02-08 07:50:17
问题 The only real tutorial I have seen for ArrayBuffer is from HTML5Rocks. But I am wondering specifically how to manipulate the individual bytes. For example, this cartoon on ArrayBuffers from Mozilla shows an image of an ArrayBuffer wrapped in a Uint8Array view: It gives the feeling that you can do this with an ArrayBuffer: var x = new ArrayBuffer(10) x[0] = 1 x[1] = 0 ... x[9] = 1 That is, manually setting the bytes. But I haven't seen any documentation on such a feature. Instead, it seems you

How to use ArrayBuffers with DataViews in JavaScript

安稳与你 提交于 2021-02-08 07:49:16
问题 The only real tutorial I have seen for ArrayBuffer is from HTML5Rocks. But I am wondering specifically how to manipulate the individual bytes. For example, this cartoon on ArrayBuffers from Mozilla shows an image of an ArrayBuffer wrapped in a Uint8Array view: It gives the feeling that you can do this with an ArrayBuffer: var x = new ArrayBuffer(10) x[0] = 1 x[1] = 0 ... x[9] = 1 That is, manually setting the bytes. But I haven't seen any documentation on such a feature. Instead, it seems you

How can an (int) be converted to (unsigned int) while preserving the original bit pattern?

老子叫甜甜 提交于 2021-02-07 07:12:11
问题 Suppose that we define: short x = -1; unsigned short y = (unsigned short) x; According to the C99 standard: Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (ISO/IEC 9899:1999 6.3.1.3/2) So, assuming two bytes for short and a two's complement representation, the bit patterns of these two integers are: x = 1111 1111 1111 1111