![]() The result b3 and b4 would look like the following. ![]() The first and second halves of the inputs are done separate so the result is 2 bytes. I want to mix them so that every first bit is from b1 while the other is from b2 (reading left to right). 'Dim resultAsBinaryString = result.In Java, I have 2 bytes: byte b1 = (byte) 0b11111111, b2 = (byte) 0b00000000 'First clear (And Not) the bits to replace, then set (Or) them.'ĭim result = (enumValue And Not mask) Or shiftedBits 'Dim shiftedBitsAsBinaryString = shiftedBits.ToBinaryString' 'Shift the bits to left to the desired position.' 'Dim shiftedMaskAsBinaryString = mask.ToBinaryString' 'Shift the mask to left to the desired position' 'Dim maskAsBinaryString = mask.ToBinaryString' 'Create a bitmask (a series of ones for the bits to retain and a series of zeroes for bits to discard).' Throw New ArgumentOutOfRangeException(String.Format("The position. Public Function PlaceBits(enumValue As Integer, bits As Integer, length As Integer, position As Integer) As Integer Apply logical OR () to the output of all the above expression, to get the desired output. Move the middle bytes using the combination of left shift and right shift operator. ''' The exponent of 2 where the bits must be placed. Move the position of the first byte to the last byte using right shift operator (>). '''The number of bits that the bits should replace. It must be smaller or equal to 2 to the power of the position parameter. ''' The integer value to place the bits in. ''' Replace the bits in the enumValue with the bits in the bits parameter, starting from the position that corresponds to 2 to the power of the position parameter. For those who are wondering what the ToBinaryString extension looks like: Convert.ToString(value,2) ''' The question was about how to implement it in C, but as all searches for "replace bits" lead to here, I will supply my implementation in VB.NET. If we have the same mask = 0x02, then we can prepare this mask for clearing by ~mask: mask = 0 0 0 0 0 0 1 0 All 1's in all other bit positions ensure that during AND the target preserves its 0's and 1's as they were in those locations, and a 0 in the bit position to be cleared would also set that bit position 0 in the target word. To clear a bit, you need to AND the target word with another word with 0 in that specific bit position and 1 in all. If we have mask = 0x02 = 00000010 (1 byte) then we can OR this to any word to set that bit position: target = 1 0 1 1 0 1 0 0 All 0's in the other positions ensure that the existing 1's in the target are as it is during OR, and the 1 in the specific positions ensures that the target gets the 1 in that position. To set a bit, you need to OR the target word with another word with 1 in that specific bit position and 0 in all other with the the target. It simply takes the index, uses the lower three bits of the index to identify eight different bit positions inside each location of the char array, and the upper remainder bits addresses in which array location does the bit denoted by x occur. Here is a generic process which acts on a long array, considering it a long bitfield, and addresses each bit position individually: #define set_bit(arr,x) ((arr) |= (0x01 >3] &= ~(0x01 >3]) & (0x01 << ((x) & 0x07))) != 0) ![]() Setting a bit is okay, but more than one bit, I am finding it little difficult. ![]() How do I replace only certain bit/bits in a 32/64 bit fields without affecting the whole data of the field using C?.How do I go about doing read, modify, and write?.If I do this, the value at bit positions 5 and 6 will become 11 (0x3), not 01 (0x1) which I wanted. Reg_data |= 1 << shift // In this case, 'shift' is 5 (Right now it is 10, which is 2 in decimal, and I want to replace it to 1 e 01) without other bits getting affected and write back the register with only bits 5 and 6 modified (so it becomes 126 after changing). Now I want to change the value at bit position 5 and 6 to 01. Now, when I read the register, I get say value 0x146 (0001 0 10 0 0110). I have a 64-bit register where bits 5 and 6 can take values 0, 1, 2, and 3. I wanted to replace bit/bits (more than one) in a 32/64 bit data field without affecting other bits. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |