Professional Documents
Culture Documents
BitFieldsinC
CBitFields
Advertisements
PreviousPage
NextPage
SupposeyourCprogramcontainsanumberofTRUE/FALSEvariablesgroupedinastructurecalledstatus,asfollows
struct{
unsignedintwidthValidated;
unsignedintheightValidated;
}status;
This structure requires 8 bytes of memory space but in actual, we are going to store either 0 or 1 in each of the variables. The C
programminglanguageoffersabetterwaytoutilizethememoryspaceinsuchsituations.
IfyouareusingsuchvariablesinsideastructurethenyoucandefinethewidthofavariablewhichtellstheCcompilerthatyouaregoing
touseonlythosenumberofbytes.Forexample,theabovestructurecanberewrittenasfollows
struct{
unsignedintwidthValidated:1;
unsignedintheightValidated:1;
}status;
Theabovestructurerequires4bytesofmemoryspaceforstatusvariable,butonly2bitswillbeusedtostorethevalues.
Ifyouwilluseupto32variableseachonewithawidthof1bit,thenalsothestatusstructurewilluse4bytes.Howeverassoonasyou
have 33 variables, it will allocate the next slot of the memory and it will start using 8 bytes. Let us check the following example to
understandtheconcept
#include<stdio.h>
#include<string.h>
/*definesimplestructure*/
struct{
unsignedintwidthValidated;
unsignedintheightValidated;
}status1;
/*defineastructurewithbitfields*/
struct{
unsignedintwidthValidated:1;
unsignedintheightValidated:1;
}status2;
intmain(){
printf("Memorysizeoccupiedbystatus1:%d\n",sizeof(status1));
printf("Memorysizeoccupiedbystatus2:%d\n",sizeof(status2));
return0;
}
Whentheabovecodeiscompiledandexecuted,itproducesthefollowingresult
Memorysizeoccupiedbystatus1:8
Memorysizeoccupiedbystatus2:4
BitFieldDeclaration
Thedeclarationofabitfieldhasthefollowingforminsideastructure
struct{
type[member_name]:width;
};
Thefollowingtabledescribesthevariableelementsofabitfield
Elements
Description
type
Anintegertypethatdetermineshowabitfield'svalueisinterpreted.Thetypemaybeint,signedint,or
unsignedint.
member_name
Thenameofthebitfield.
width
Thenumberofbitsinthebitfield.Thewidthmustbelessthanorequaltothebitwidthofthespecifiedtype.
https://www.tutorialspoint.com/cprogramming/c_bit_fields.htm
1/2
13/11/2016
BitFieldsinC
Thevariablesdefinedwithapredefinedwidtharecalledbitfields.Abitfieldcanholdmorethanasinglebitforexample,ifyouneeda
variabletostoreavaluefrom0to7,thenyoucandefineabitfieldwithawidthof3bitsasfollows
struct{
unsignedintage:3;
}Age;
TheabovestructuredefinitioninstructstheCcompilerthattheagevariableisgoingtouseonly3bitstostorethevalue.Ifyoutrytouse
morethan3bits,thenitwillnotallowyoutodoso.Letustrythefollowingexample
#include<stdio.h>
#include<string.h>
struct{
unsignedintage:3;
}Age;
intmain(){
Age.age=4;
printf("Sizeof(Age):%d\n",sizeof(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);
return0;
}
Whentheabovecodeiscompileditwillcompilewithawarningandwhenexecuted,itproducesthefollowingresult
Sizeof(Age):4
Age.age:4
Age.age:7
Age.age:0
PreviousPage
NextPage
Advertisements
Write for us
FAQ's
Helping
Contact
https://www.tutorialspoint.com/cprogramming/c_bit_fields.htm
go
2/2