×

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall within a certain range and not exceed a certain threshold.

Example:

#include<stdio.h>
         //presentation of data done
         struct date {
//declaring variables
             unsigned int d;
                unsigned int m;
                unsigned int y;
        };
        int main ()
        {
printf (“size of date is %lu bytes \n”,  //printing the statement 
                              Size of (struct date));
               struct date dt = {31, 12,2014};
printf (“Date is %d%d%d”, dt. d, dt.m, dt.y);
         }

Output:

Bit Fields in C

From the above code, we know that the date takes 12 bytes but as we know 4 bytes is taken by unsigned int and we also know date is always from the values 1 to 31 and month is 1 to 12.

Example using signed integers

#include<stdio.h>
//space develop for presentation of date
struct date {
              // d taking values between the range of 0 to 31 bytes, 5 bits
              int d:5;
       // m takes Values between 0 to 12, so 4 bits
              int m:4;
              int y;
   };
         int main ()
{ 
printf (“date size is %lu bytes \n”, size of (struct date));
              struct date dt = {31,12,2014};
printf (“date is %d%d%d”, dt. ,dt.m, dt. y);
               return 0;
           }

Output:

Bit Fields in C

From the above result, we can see that the value obtained is negative because as 31 is equals to 11111 as it is stored in 5-bit signed integer and the most significant bit is 1, to obtain its original value we should calculate it by 2’s compliment, after calculating we obtain 00001 which is equals to decimal number 1 so as the number is negative it shows -1. Similarly, it happens with 12 equals to 1100 stored in 4-bit Signed int by calculating with 2’s compliment we get -4.

Amazing facts related to bit fields in C

  • To force alignment special unnamed of bit field, size 0 is used on another boundary

Example:

#include<stdio.h>
      //presentation of structure without an alignment force
      struct test1 {
unsigned int x: 5;
              unsigned int y:8;
       };
       //presentation of structure with an alignment force
       struct test2 {
               unsigned int x:5;
               unsigned int:0;
                unsigned int y: 8;
         };
         int main ()
         {
printf(“size of test1 is %lu bytes\n”, size of (struct test 1));
printf (“size of test2 is %lu bytes\n”, size of (struct test 2));
                 return 0;
           }

Output:

Bit Fields in C
  • As they do not start at byte boundary,we may not have pointers to bit field members:

Example:

#include<stdio.h>
            struct test {
                   unsigned int x:5;
                   unsigned int y:5;
unsigned int z;
 };
             int main ()
             {
                   struct test t;
//Leaving a line uncommented causes the programme to build and run.
printf (“address of t.x is %p”, &t.x);


                  //as z is not a member of bit field so below line works fine
printf (“address of t.z is %p”, &t. z);
                    return 0;
              }

Output:

Bit Fields in C

Bit field declaration

 Structure of the bit field declaration:

struct {
          type [member name] : width;
};

The below table shows the variable elements of bit field:

Sr. NoElements & Description
   1.Type: An int type that determines how bit fields clarify and the type may be unsigned or singed int.
2.Member _ name: Bit field name
3.Width: The bit width of specified type must be greater than or equal to Width and the width are bits number in bit field

Bit fields can be defined as variables with predefined width, and it can hold more than one bit.

Example: For storing values from 0 to 7 variable is needed and we should define bit field with width 3

struct {
            unsigned int age: 3;
       } Age;

If we use more than 3 bits, then it will not be allowed.

Example:

#include<stdio.h>
         #include<string.h>
         struct {
               unsigned int age: 3;
          } Age;
         int main () {
Age. Age=4;
printf (“size of (Age): %d\n”, size of (Age));
printf (“Age. age: %d\n”, Age. age);
Age.age=7;
printf (“Age. age: %d\n”, Age. age);
Age.age =8;
printf (“Age. age: %d\n”, Age. age);
return 0;
}

Output:

Bit Fields in C

Related Topics

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.