bit-manipulation

Infinite loop while adding two integers using bitwise operations in Python 3

假装没事ソ 提交于 2020-01-03 15:32:26
问题 I am trying to solve a question which is about writing python code for adding two integers without the use of '+' or '-' operators. I have the following code which works perfectly for two positive numbers: def getSum(self, a, b): while (a & b): x = a & b y = a ^ b a = x << 1 b = y return a ^ b This piece of code works perfectly for if input is two positive integers or two negative integers but it fails when one number is positive and other is negative. It goes into infinite loop. Any idea as

Infinite loop while adding two integers using bitwise operations in Python 3

左心房为你撑大大i 提交于 2020-01-03 15:32:25
问题 I am trying to solve a question which is about writing python code for adding two integers without the use of '+' or '-' operators. I have the following code which works perfectly for two positive numbers: def getSum(self, a, b): while (a & b): x = a & b y = a ^ b a = x << 1 b = y return a ^ b This piece of code works perfectly for if input is two positive integers or two negative integers but it fails when one number is positive and other is negative. It goes into infinite loop. Any idea as

Bitwise operations in Pandas that return numbers rather than bools?

删除回忆录丶 提交于 2020-01-03 14:12:30
问题 Question How can I perform bitwise operations in Pandas? How & works on integers On integers the & operator performs a bitwise mask >>> mask = 0b1100 # 4 and 8 bits on >>> 7 & mask 4 How & works in Pandas Is there some way to perform bitwise masking operations in Pandas? The & operator does something else. >>> df = DataFrame([1, 2, 3, 4, 5, 6, 7, 8], columns=['data']) >>> df.data & mask 0 False 1 False 2 False 3 True 4 True 5 True 6 True 7 True Name: data, dtype: bool 回答1: In [184]: df = pd

Random numbers between 1 and 15

一个人想着一个人 提交于 2020-01-03 13:39:18
问题 I generate many many random numbers that need to be between 1 and 15 (included) in C++. Of course, I can generate zillons of std::uniform_int_distribution<std::mt19937::result_type> random(1, 15); but this is a waste since this mersenn twister generates 32 bits (or even 64 using mt19937_64) of random values, and I would only keep 4 bits and throw away all the rest, and in my case, performance is an issue and random number generation is a significant contributor. My idea was thus to generate

How to do bit striping on pixel data?

微笑、不失礼 提交于 2020-01-03 08:52:48
问题 I have 3 buffers containing R, G, B bit data running on a 32-bit processor. I need to combine the three bytes in the following way: R[0] = 0b r1r2r3r4r5r6r7r8 G[0] = 0b g1g2g3g4g5g6g7g8 B[0] = 0b b1b2b3b4b5b6b7b8 int32_t Out = 0b r1g1b1r2g2b2r3g3 b3r4g4b4r5g5b5r6 g6b6r7g7b7r8g8b8 xxxxxxxx where xxxxxxxx is continuing on to each of the next bytes in the buffers. I am looking for an optimal way to combine them. My approach is definitely not efficient. Here is my approach static void

How to do bit striping on pixel data?

流过昼夜 提交于 2020-01-03 08:52:42
问题 I have 3 buffers containing R, G, B bit data running on a 32-bit processor. I need to combine the three bytes in the following way: R[0] = 0b r1r2r3r4r5r6r7r8 G[0] = 0b g1g2g3g4g5g6g7g8 B[0] = 0b b1b2b3b4b5b6b7b8 int32_t Out = 0b r1g1b1r2g2b2r3g3 b3r4g4b4r5g5b5r6 g6b6r7g7b7r8g8b8 xxxxxxxx where xxxxxxxx is continuing on to each of the next bytes in the buffers. I am looking for an optimal way to combine them. My approach is definitely not efficient. Here is my approach static void

C# bitwise shift on ushort (UInt16)

淺唱寂寞╮ 提交于 2020-01-03 08:14:12
问题 I need to perform a bitwise left shift on a 16-bit integer (ushort / UInt16), but the bitwise operators in C# seem to apply to int (32-bit) only. How can I use << on an ushort, or at least get to the same result with a simple workaround? 回答1: Cast the resulting value back into ushort after shifting: ushort value = 1; ushort shifted = (ushort)(value << 2); 来源: https://stackoverflow.com/questions/3819593/c-sharp-bitwise-shift-on-ushort-uint16

c# - Get Specific Bit and Get first 14 bits of a ushort value

微笑、不失礼 提交于 2020-01-03 06:34:11
问题 After reading all of the questions and answers on bit shifting/masking, I simply cannot wrap my head around it. I'm just not understanding how it works on a fundamental level. I've been able to achieve various techniques by using BitArray and BitConverter instead, but I really would like to understand bit shifting/masking better. The specific need I have is to do the following: I have a ushort: 0x810E (33038) Using bit shifting/masking, I'd like to know how to: Get the 16th bit Result: 1 Get

Subset of array A in which if we do AND of all elements of that subset then output should be in power of two

房东的猫 提交于 2020-01-03 04:46:11
问题 I got this solution to a problem where it states: Given an array A. Is there any subset of array A in which if we do AND of all elements of that subset then output should be in power of two (for example : 1,2,4,8,16 and so on ). Input: First line contains number of test cases T. Each test first line contains N size of array A and next line contains N space separated integers. Output: For each test case print YES if there is any subset of array A in which if we do AND of all elements of that

Shift operations

与世无争的帅哥 提交于 2020-01-03 04:32:10
问题 I saw the following posted by one of the fellow stackoverflower and it sort of dumbfounds me. Would someone explain the shifting operations in the following code snippet: std::vector<bool> a; a.push_back(true); a.push_back(false); //... for (auto it = a.begin(); it != a.end();) // see 0x for meaning of auto { unsigned b = 0; for (int i = 0; i < 8*sizeof(b); ++i) { b |= (*it & 1) << (8*sizeof(b) - 1 - i); ++it; } // flush 'b' } 回答1: 8 * sizeof(b) is the number of bits that can be stored in 'b'