Bit fields in C



Share



Learn C bit fields, bit manipulation, advantages of bit field, shift operator, bitwise operators such as or, xor, not, right shift, left shift with examples, demonstrations.

What is bit field in C?

Bit FieldsWith C you can manipulate individual bits in a variable. Sometimes this is necessary. When programming microcontrollers you must learn how each bit is to be paid attention. Often we must store the condition 0 or 1 in a variable only. If we take to the smallest well-known data types, i.e. unsigned char, now for the storage of an individual value that needs a single bit, we waste 7 bits, since unsigned char an 8-bit is broad.

Structural padding:The second method of manipulating bits is to use a bit field within a structure. Architecture of a computer processor is such a way that it can read 1 word (4 byte in 32 bit processor) from memory at a time. To make use of this advantage of processor, data are always aligned as 4 bytes package which leads to insert empty addresses between other member’s address. Because of this structure padding concept in C, size of the structure is always not same as what we think. A bit field is used within a structure declaration that labels each field and determines its width. Bit fields within structure are used to save memory space by using bits.

Bitwise shift operators: Bitwise shift operators are widely used in the embedded systems in situations where we need to set/clear/toggle just one single bit of a specific register without modifying the other contents. We can do OR/AND/XOR operations with the appropriate mask for the bit position. Following are the Bitwise shift operators and their functionality.

  • << left shift -----> i << flags is the result when bits in i are shifted left by flags bits and for each bit shifted left is filled by 0's. Left shift is equivalent to multiplying the bit pattern with 2\^k ( if we are shifting k bits 1 << k)
  • >> right shift -----> i >> flags is the result when bits in i are shifted right by flags bits and for each bit shifted right is filled by 0's. Right shift is equivalent to dividing the bit pattern with 2k ( if we are shifting k bits 1 >> k ).
  • ~ bitwise complement ---> ~ operator produces complement of its operand.
  • & bit wise and ---> & operator produces a boolean and operation on all corresponding bits on two operands.
  • ^ bit wise exclusive or ^ operator produces 0 whenever both operands have a 1bit.
  • | bit wise inclusive or \^ operator produces 1 whenever both operands have a 1bit
  • Setting a bit

    Let's write a small program to set bit x in a 32 bit number as shown below.

    
    #include "stdio.h"
    unsigned int SetBit(unsigned int ,unsigned int );
    void main()
    {
      unsigned int number = 0xFFFFFFF0;
      number = SetBit(number,0);
      printf("SetBit Result is 0x%x\n",number);
    }
    unsigned int SetBit(unsigned int num,unsigned int x)
    {
      return num |= 1 << x;
    }
    
    Program output:
    ==============
    $ gcc prog.c
    $ ./a.out
    SetBit Result is 0xfffffff1
    $
    

    Clearing one or more bits

    Let's write a small program to clear bit x in a 32 bit number as shown below.

    
    #include "stdio.h"
    unsigned int ClearBit(unsigned int ,unsigned int );
    void main()
    {
      unsigned int number = 0xfffffff1;
      number = ClearBit(number,0);
      printf("ClearBit Result is 0x%x\n",number);
    }
    unsigned int ClearBit(unsigned int num,unsigned int x)
    {
      return num &= ~(1 << x);
    }
    
    Program output:
    ==============
    $ gcc prog.c
    $ ./a.out
    ClearBit Result is 0xfffffff0
    $
    
  • Clearing more than 1 bit
  • Let's write a small program to clear more than 1 bit at a time in a 32 bit number as shown below.

    
    #include "stdio.h"
    #define START 24
    #define END 31
    unsigned int ClearBits(unsigned int ,unsigned int ,unsigned int );
    void main()
    {
      unsigned int number = 0xFFFFFFF0;
      number = ClearBits(number,START,END);
      printf("ClearBits result is 0x%x\n",number);
    }
    unsigned int ClearBits(unsigned int num,unsigned int start,unsigned int end)
    {
      unsigned int i;
      for(i = start;i <= end;i++)
      {
        num &= ~(1 << i);
      }
      return num;
    }
    
    Program output:
    ==============
    $ gcc prog.c
    $ ./a.out
    ClearBits result is 0xfffff0
    $
    

    Toggling a bit

    Let's write a small program to toggle bit x in a 32 bit number as shown below.

    
    #include "stdio.h"
    unsigned int ToggleBit(unsigned int ,unsigned int );
    void main()
    {
      unsigned int number = 0xfffffff0;
      number = ToggleBit(number,0);
      printf("ToggleBit result is 0x%x\n",number);
      number = ToggleBit(number,0);
      printf("ToggleBit result is 0x%x\n",number);
    }
    unsigned int ToggleBit(unsigned int num,unsigned int x)
    {
      return num ^= (1 << x);
    }
    
    Program output:
    ==============
    $ gcc prog.c
    $ ./a.out
    ToggleBit result is 0xfffffff1
    ToggleBit result is 0xfffffff0
    $
    

    To Read Full Article

    You need to subscribe & logged in

    Subscribe

    Exercises, Solutions

    High
    High
    Medium
    Medium
    Medium
    Medium
    High

    Want to contribute a new article? Write and upload your article information .
    Share

     Articles

     C Programming

     Linux Software

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software