Deep C Dives: Bits! |
Written by Mike James | |||||||
Wednesday, 09 April 2025 | |||||||
Page 3 of 3
A Single Function To Write BitsLet’s combine these operations into a single bitWrite operation that uses a mask to specify the bits to change and a data value to specify what each bit should be set to. If you regard the mask as the addresses of the bits to be changed then this writes the data to those bits and only those bits. For example: int mask = 0x03; //011 specifies that the first two bits should be modified and: int data = 0x02; //010 specifies that the first bit should be set to zero and the second should be set to one. The trick to working out how to do this is to construct one mask to set the bits that need to be set and another to unset the bits that need to be unset. Then if a bit is to be set, it needs a 1 in the mask and a 1 in the data and the mask to set bits is: setmask = mask & data If a bit is to be unset it needs a 1 in the mask and 0 in the data, so the mask to reset bits is: resetmask = mask & ~data Applying both to the value gives the required result: (value | setmask) & ~(resetmask) = (value | (mask & data)) & ~(mask & ~ data) which, after simplification, is: value & ~mask | mask & data Using this it is easy to create a function to do the job: int bitWrite(int value, int mask, int data) { value = value & ~mask | mask & data; return value; } For example: printf("%b\n", bitWrite(0xFF, 0x0F, 0x05)); The mask means that only the first four bits are modified and the data sets them to 0101 which is the result of 11110101. You can use bitWrite to set any group of bits to any value. Reading BitsSo far we have focused on writing bits, but we also often want to read a bit pattern and the method is more or less the same and relies on masks. As before, if you create a mask with bits set corresponding to the bits you want to read, then: value & mask returns 0 if and only if all of the bits the mask specifies are 0. Similarly: ~ value & mask returns 0 if and only if all of the bits the mask specifies are 1. Usually, you only want to test for a single bit. For example: int value = 0x1F; int mask = 0x10; int result = ~value & mask; tests the fifth bit in value which is 1 and so result is 0. If you test for the fifth bit to be a zero using: int value = 0x1F; int mask = 0x10; int result = value & mask; then the result is non-zero, 16 to be precise. If you want to convert the test results to Boolean values you can use the logical operator !. For example: !(~value & mask) is true if and only if all of the bits in value specified by mask are 1 and false otherwise. Similarly: !(value & mask) is true if and only if all of the bits in the value specified by mask are 0 and false otherwise. If you want to just read the bits specified by the mask, simply AND it with the value: value & mask For example, to read the bottom four bits you can use: result = value & 0x0F; In dive but not in this extract
Deep C Dives
|
Google Announces Application Design Center 10/04/2025 Google has announced the public preview of Application Design Center, a service that combines combines Gemini Cloud Assist chat with a visual, canvas-style interface for app development. |
Understanding GPU Architecture With Cornell 11/04/2025 Find out everything there's to know about GPUs. This Cornell Virtual Workshop will be helpful for those who program in CUDA. |
More News
|
Comments
or email your comment to: comments@i-programmer.info