Deep C Dives: Bits!
Written by Mike James   
Wednesday, 09 April 2025
Article Index
Deep C Dives: Bits!
The Bitwise Operators
A Single Function To Write Bits

A Single Function To Write Bits

Let’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 Bits

So 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

  • Shifting Values
  • Significant Bits
  • Rotate
  • Bit Fields
  • Some Examples
      Count the Bits
      Largest Power of 2 that Divides 
      Parity
      Overflow-Free Average

Deep C Dives
Adventures in C

By Mike James

Cdive360

Buy from Amazon.

Contents

Preface
Prolog C
Dive

  1. All You Need Are Bits
  2. These aren’t the types you’re looking for
  3. Type Casting
  4. Expressions
  5. Bits and More Bits
        Extract:
    Bits! ***NEW!
  6. The Brilliant But Evil for 
  7. Into the Void 
  8. Blocks, Stacks and Locals
  9. Static Storage
  10. Pointers
  11. The Array and Pointer Arithmetic
  12. Heap, The Third Memory Allocation
  13. First Class Functions
        Extract:
    First Class Functions
  14. Structs and Objects
  15. The Union
  16. Undefined Behavior
  17. Exceptions and the Long Jump

<ASIN:B0D6LZZQ8R>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


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

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Wednesday, 09 April 2025 )