bit-manipulation

Counting the number of leading zeros in a 128-bit integer

最后都变了- 提交于 2019-12-09 00:42:53
问题 How can I count the number of leading zeros in a 128-bit integer ( uint128_t ) efficiently? I know GCC's built-in functions: __builtin_clz , __builtin_clzl , __builtin_clzll __builtin_ffs , __builtin_ffsl , __builtin_ffsll However, these functions only work with 32- and 64-bit integers. I also found some SSE instructions: __lzcnt16 , __lzcnt , __lzcnt64 As you may guess, these only work with 16-, 32- and 64-bit integers. Is there any similar, efficient built-in functionality for 128-bit

What part of integer bit-representation is part of The Standard? [duplicate]

谁都会走 提交于 2019-12-08 19:37:28
This question already has answers here : What do the C and C++ standards say about bit-level integer representation and manipulation? (8 answers) What does the C++ standard state the size of int, long type to be? (24 answers) Closed 4 years ago . How much can one assume about integers' representation in memory? How portable way of slicing and splicing integers together are bit-wise operations? The C standard discusses the representations of integer types in section 6.2.6.2. It specifies a binary representation for integer types. For unsigned types, the bits are divided into value bits and

toggle a bit at ith positon [duplicate]

一个人想着一个人 提交于 2019-12-08 18:24:51
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: How do you set, clear and toggle a single bit in C? Can some one help me how to toggle a bit at ith position. One way is to do ((n>>i) ^ 1) << i . Are there any other ways ? 回答1: n ^= 1U << i is easy enough, isn't it? 回答2: You could do pow(2, i) ^ n 来源: https://stackoverflow.com/questions/3681684/toggle-a-bit-at-ith-positon

Why does bit-shifting an int upwards produce a negative number?

江枫思渺然 提交于 2019-12-08 17:45:05
问题 I am new to bit manipulations tricks and I wrote a simple code to see the output of doing single bit shifts on a single number viz. 2 #include <iostream> int main(int argc, char *argv[]) { int num=2; do { std::cout<<num<<std::endl; num=num<<1;//Left shift by 1 bit. } while (num!=0); return 0; } The output of this is the following. 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456

bit count function in K&R [closed]

拈花ヽ惹草 提交于 2019-12-08 17:39:45
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . In the book "C Programming Language" by K&R, there is a bit count function: int bitsCount(unsigned x) { int b; for (b = 0; x != 0; x >>= 1) if (x & 01) b++; return b; } My question is why they use x & 01 but not x & 1 or x & 00000001 ? Isn't that 01 means octal 1? 回答1:

Move integer to the nearest divisible by 4 in C++

好久不见. 提交于 2019-12-08 17:33:55
问题 I'd like to move an integer up to the nearest 'divisible by 4' number in C++, but not if the number is currently divisible by 4. Here's my best attempt, but I'm sure this is suboptimal: offset = (offset & 3) ? (offset | 3) +1 : offset; There must surely be a fast way of doing this that doesn't include the tertiary IF statement? Additional note: In this case offset is a 32-bit int. 回答1: offset = (offset + 3) & ~(decltype(offset))3; Or #include <type_traits> // ... offset = (offset + 3) &

Return zero for negative integers

∥☆過路亽.° 提交于 2019-12-08 16:37:38
问题 A friend just throw some code similar to following C# code: int i = ...; return i < 0 ? 0 : i; That made me think. There's any "different" way to return zero for negative integers, or current positive value? More specifically I'm looking for bitwise operations, if possible. BTW, I'm aware of Math.Max(0, i); 回答1: What's wrong with Math.Max ? You can do the equivalent without a branch using bitwise operations: r = x ^ ((x ^ y) & -(x < y)); // == max(x, y) If you substitute zero, it collapses to

Reversibly encode two large integers of different bit lengths into one integer

强颜欢笑 提交于 2019-12-08 15:02:53
问题 I want to encode two large integers of possibly different maximum bit lengths into a single integer. The first integer is signed (can be negative) whereas the second is unsigned (always non-negative). If the bit lengths are m and n respectively, the bit length of the returned integer should be less than or equal to m + n . Just n (but not m ) is known in advance and is fixed. The solution will as an example be used to combine a signed nanosecond timestamp of 61+ bits along with 256 bits of

What is lower and higher bits?

烈酒焚心 提交于 2019-12-08 14:55:57
问题 Can anyone tell me what is lower and higher bits?. How to identify a higher and lower bit?. Below is binary form. How does 0110 is higher bit in it?. 0110 0111 1100 1010 1100 0111 1001 1011 回答1: Just like in decimal, higher places are generally written to the left in binary. So if you see 0111 , the 0 is the high bit. So this would represent 7 in decimal. The same applies when spaces are used, just like when commas (or dots, depending on your locale) are used to separate groups of digits in

C# - fast way to compare 2 integers, bit by bit, and output for more than one integer, possible?

房东的猫 提交于 2019-12-08 14:20:19
问题 I have two input integer numbers and an output list< int> myoutputlist. My inputs are lets say A=0x 110101 B=0x 101100 then I have calculated a C integer number depending on A and B numbers.I already coded its algorithm, I can calculate C integer. C integer shows which bits should be changed. 1 value represents changing bits, 0 values represents unchanging bits. Only one bit should be changed in each time. As C integer depends on A and B inputs, sometimes 1 bit, sometimes 3 bits, sometimes 8