bitwise-operators

Filpping bits in binary number

与世无争的帅哥 提交于 2020-01-07 03:04:18
问题 Can anyone provide a clear logic for the below problem. I am stuck with confusion. An n bit number is given as input and OP(j) and OP(k) are applied on it one after the other. Objective is to specify how many bits will remain the same after applying these two operations. OP(i) implies flipping of each ith bit. i > 0 回答1: Based on your description, the operation OP(i) will change every 'i'th bit, so it changes a total of floor(n / i) bits. Chaining the operation makes things tricky. If the

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 来源:

Java Bit Operations: replacing nibble of hex [duplicate]

谁说胖子不能爱 提交于 2020-01-06 08:10:34
问题 This question already has answers here : set 4-bit nibble in an int type (3 answers) Closed 3 years ago . I have to write this code for a homework assignment but I don't even know where to start. Here is the javadoc to the method I have to write. /** * Sets a 4-bit nibble in an int * Ints are made of eight bytes, numbered like so: 7777 6666 5555 4444 3333 2222 1111 0000 * * For a graphical representation of this: * 1 1 1 1 1 1 * 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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

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