Share | Tweet |
---|

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.

**Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs**

**Bit Fields**With 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.

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 $

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 $

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 $

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 $

Share | Tweet |
---|