bit-manipulation

Bitwise operator for simply flipping all bits in an integer?

眉间皱痕 提交于 2019-12-17 10:35:23
问题 I have to flip all bits in a binary representation of an integer. Given: 10101 The output should be 01010 What is the bitwise operator to accomplish this when used with an integer? For example, if I were writing a method like int flipBits(int n); , what would go in the body? I need to flip only what's already present in the number, not all 32 bits in the integer. 回答1: The ~ unary operator is bitwise negation. If you need fewer bits than what fits in an int then you'll need to mask it with &

Bits counting algorithm (Brian Kernighan) in an integer time complexity

怎甘沉沦 提交于 2019-12-17 10:22:30
问题 Can someone explains why Brian Kernighan's algorithm takes O(log N) to count set bits (1s) in an integer. A simple implementation of this algorithm is below (in JAVA) int count_set_bits(int n){ int count = 0; while(n != 0){ n &= (n-1); count++; } return count; } I understand how it works by clearing the rightmost set bit one by one until it becomes 0, but I just don't know how we get O(log N). 回答1: This algorithm goes through as many iterations as there are set bits. So if we have a 32-bit

bitwise XOR of hex numbers in python

a 夏天 提交于 2019-12-17 10:22:20
问题 how can we XOR hex numbers in python eg. I want to xor 'ABCD' to '12EF'. answer should be B922. i used below code but it is returning garbage value def strxor(a, b): # xor two strings of different lengths if len(a) > len(b): return "".join(["%s" % (ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)]) else: return "".join(["%s" % (ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])]) key ='12ef' m1='abcd' print strxor(key,m1) 回答1: Whoa. You're really over-complicating it by a very long distance.

How to manually (bitwise) perform (float)x?

隐身守侯 提交于 2019-12-17 09:54:11
问题 Now, here is the function header of the function I'm supposed to implement: /* * float_from_int - Return bit-level equivalent of expression (float) x * Result is returned as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point values. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ unsigned float_from_int(int x) { ... } We aren't allowed to do float operations, or any kind of

How do I get bit-by-bit data from an integer value in C?

血红的双手。 提交于 2019-12-17 08:01:57
问题 I want to extract bits of a decimal number. For example, 7 is binary 0111, and I want to get 0 1 1 1 all bits stored in bool. How can I do so? OK, a loop is not a good option, can I do something else for this? 回答1: If you want the k-th bit of n, then do (n & ( 1 << k )) >> k Here we create a mask, apply the mask to n, and then right shift the masked value to get just the bit we want. We could write it out more fully as: int mask = 1 << k; int masked_n = n & mask; int thebit = masked_n >> k;

Unexpected C/C++ bitwise shift operators outcome

旧街凉风 提交于 2019-12-17 07:49:42
问题 I think I'm going insane with this. I have a a piece of code that needs to create an (unsigned) integer with N consequent bits set to 1. To be exact I have a bitmask, and in some situations I'd like to set it to a solid rnage. I have the following function: void MaskAddRange(UINT& mask, UINT first, UINT count) { mask |= ((1 << count) - 1) << first; } In simple words: 1 << count in binary representation is 100...000 (number of zeroes is count ), subtracting 1 from such a number gives 011...111

Efficient bitwise operations for counting bits or find the right|left most ones

夙愿已清 提交于 2019-12-17 06:49:10
问题 Given an unsigned int, I have to implement the following operations : Count the number of bits set to 1 Find the index of the left-most 1 bit Find the index of the righ-most 1 bit (the operation should not be architecture dependents). I've done this using bitwise shift, but I have to iterate through almost all the bits(es.32) . For example, counting 1's: unsigned int number= ...; while(number != 0){ if ((number & 0x01) != 0) ++count; number >>=1; } The others operation are similar. So my

PHP function flags, how?

扶醉桌前 提交于 2019-12-17 06:41:40
问题 I'm attempting to create a function with flags as its arguments but the output is always different with what's expected : define("FLAG_A", 1); define("FLAG_B", 4); define("FLAG_C", 7); function test_flags($flags) { if($flags & FLAG_A) echo "A"; if($flags & FLAG_B) echo "B"; if($flags & FLAG_C) echo "C"; } test_flags(FLAG_B | FLAG_C); # Output is always ABC, not BC How can I fix this problem? 回答1: Flags must be powers of 2 in order to bitwise-or together properly. define("FLAG_A", 0x1); define

Count number of bits in a 64-bit (long, big) integer?

三世轮回 提交于 2019-12-17 06:36:30
问题 I have read through this SO question about 32-bits, but what about 64-bit numbers? Should I just mask the upper and lower 4 bytes, perform the count on the 32-bits and then add them together? 回答1: You can find 64 bit version here http://en.wikipedia.org/wiki/Hamming_weight It is something like this static long NumberOfSetBits(long i) { i = i - ((i >> 1) & 0x5555555555555555); i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333); return (((i + (i >> 4)) & 0xF0F0F0F0F0F0F0F) *

Count number of bits in a 64-bit (long, big) integer?

亡梦爱人 提交于 2019-12-17 06:36:24
问题 I have read through this SO question about 32-bits, but what about 64-bit numbers? Should I just mask the upper and lower 4 bytes, perform the count on the 32-bits and then add them together? 回答1: You can find 64 bit version here http://en.wikipedia.org/wiki/Hamming_weight It is something like this static long NumberOfSetBits(long i) { i = i - ((i >> 1) & 0x5555555555555555); i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333); return (((i + (i >> 4)) & 0xF0F0F0F0F0F0F0F) *