bit-manipulation

How to do “indexOf” and “lastIndexOf” on an integer with bit flags? (Get the *power* of the found index)

白昼怎懂夜的黑 提交于 2020-01-06 12:51:20
问题 This is a follow up on my previous question on bit flags, where I cleared up some big misconceptions. I need to create these functions to find a single bit-flag in an int containing zero or more flags: BitBinaryUtil int twoExponentOfHighestOneBit(int all_flags) int twoExponentOfHighestOneBitAtMost(int all_flags, int twoExponentOfTwo_max0Thr30Incl) int twoExponentOfLowestOneBit(int all_flags) int twoExponentOfLowestOneBitAtLeast(int all_flags, int twoExponentOfTwo_min0Thr30Incl) Which are

Knowing the XOR of odd Number

邮差的信 提交于 2020-01-06 09:05:41
问题 Given an array of odd numbers or even numbers, how can one efficiently get pairs of this array whose XOR == 2? For example: arr = [4,10,2,6,8] pairs are: [(4,6), (8,10)] #4^6 == 2, 8^10 == 2 Or: arr = [5,9,3,7,11] pairs are: [(9,11), (5,7),] I did this to get them (brute-force) for i in combinations(inev,2):#inev is the list of indices (positions of the numbers in the array) if not (arr[i[0]] ^ arr[i[1]]) & 1 and (arr[i[0]] ^ arr[i[1]]) == 2: print arr[i[0]], arr[i[1]] #I print the pair 来源:

Knowing the XOR of odd Number

大城市里の小女人 提交于 2020-01-06 09:05:12
问题 Given an array of odd numbers or even numbers, how can one efficiently get pairs of this array whose XOR == 2? For example: arr = [4,10,2,6,8] pairs are: [(4,6), (8,10)] #4^6 == 2, 8^10 == 2 Or: arr = [5,9,3,7,11] pairs are: [(9,11), (5,7),] I did this to get them (brute-force) for i in combinations(inev,2):#inev is the list of indices (positions of the numbers in the array) if not (arr[i[0]] ^ arr[i[1]]) & 1 and (arr[i[0]] ^ arr[i[1]]) == 2: print arr[i[0]], arr[i[1]] #I print the pair 来源:

Hiding number in large pointer array by modifying least significant bit of elements

可紊 提交于 2020-01-06 05:22:25
问题 Straight of the bat I will say I barely know what I'm doing here - I'm having a major trouble grasping bitwise operators in C. As an exercise in one of my courses I'm supposed to hide a number (unsigned int) in large pointer array (unsigned char) containing numbers. I'm doing it using srand (with key, so that I can decode it later) to choose specific elements of the array and then take one bit of the number I'm supposed to hide (iterating through all the bits) and changing the least

Hiding number in large pointer array by modifying least significant bit of elements

自古美人都是妖i 提交于 2020-01-06 05:22:07
问题 Straight of the bat I will say I barely know what I'm doing here - I'm having a major trouble grasping bitwise operators in C. As an exercise in one of my courses I'm supposed to hide a number (unsigned int) in large pointer array (unsigned char) containing numbers. I'm doing it using srand (with key, so that I can decode it later) to choose specific elements of the array and then take one bit of the number I'm supposed to hide (iterating through all the bits) and changing the least

Assigning individual bits to bytes

懵懂的女人 提交于 2020-01-06 04:51:07
问题 I have a to make a SPI communication between a microcontroller and another chip. The chip accepts a 16bit word. But the abstraction library requires the data to be sent as two 8bit bytes. Now I want to make a wrapper so I can easily create requests for read and write...but I have not yet got any success. Here is how it supposed to be: The table below shows 16bits. The MSB can be 0 for write or 1 for read. The address can be from 0x0 to 0x7 and the data is 11 bits. R/W | ADDRESS | DATA B15 |

Bitwise Operations Help/Suggestions

六眼飞鱼酱① 提交于 2020-01-05 09:36:09
问题 Alright, I'm not looking for answers or anything like that. So on recent exams, when I've been asked to perform some relatively simple bitwise operations, I just can't seem to get the job done. Given 30 minutes to an hour, I could flush it out, but with 10 minutes or less, I just get stuck. For example, i was recently asked to write a small function, if x > y,return 1, else 0. I couldnt for the life of me provide an answer. After the exam, I went home and wrote out the answer, but it took me

Fastest way to clamp a real (fixed/floating point) value?

强颜欢笑 提交于 2020-01-05 08:06:49
问题 Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be handled in separate functions. Obviously, I can do something like: double clampedA; double a = calculate(); clampedA = a > MY_MAX ? MY_MAX : a; clampedA = a < MY_MIN ? MY_MIN : a; or double a = calculate(); double clampedA = a; if(clampedA > MY_MAX

How to add a code fix for infinite loop while adding two integers using bitwise operations

耗尽温柔 提交于 2020-01-05 06:09:32
问题 Here is the original question. Here is the code for adding two integers using bitwise operations: def getSum(self, a, b): while (a & b): x = a & b y = a ^ b a = x << 1 b = y return a ^ b Though I know the reason why it goes into an infinite loop while adding a positive and negative integer, I need some help with coming up with the code-fix for this. 回答1: I am assuming that you have gone through the logic behind the infinite loop you are getting. logic Following is the behaviour of your

How to add a code fix for infinite loop while adding two integers using bitwise operations

孤者浪人 提交于 2020-01-05 06:09:25
问题 Here is the original question. Here is the code for adding two integers using bitwise operations: def getSum(self, a, b): while (a & b): x = a & b y = a ^ b a = x << 1 b = y return a ^ b Though I know the reason why it goes into an infinite loop while adding a positive and negative integer, I need some help with coming up with the code-fix for this. 回答1: I am assuming that you have gone through the logic behind the infinite loop you are getting. logic Following is the behaviour of your