Course Content

  • bitfields

Course Content

Bit Fields

Hello everyone my name is Atharva and we are continuing with this C programming course. In which, in the last video, we had discussed File Handling.

We have seen how with the help of C programming, we can handle our files which means the input and output operations that happened on our file, that is the read and write operations, we can do with the help of C programming.

This is what we had discussed in our last video.

In this video, we will talk about such a topic which helps us to efficiently use the storage space.

So, here I'm talking about ‘playing with bit field’s topic.’

Here exactly we are talking about bit fields.

What exactly are bit fields and what is it used for? And wherever we can use it and where we cannot use it.

 And how it helps to efficiently use our storage.

 All the answers to this question, we will be seeing in this video.

So, we will quickly start and we will move on to our first slide.

Here we will see what exactly bit fields are but before knowing what bit fields, we will see that when one variable is made, when it is declared, at that time the storage that is allocated for it on the hard disk storage based on the data type, the variable is declared with.

According to the data type, the storage that is declared on our hard disk, that is basically in the form of bits.

So here, you can see that we count the storage in bits and bits is the smallest unit.

We bring bits together.

If we are getting eight bits together, then it becomes one byte.

So, if I talk about that we're talking about the 5 or 10 bytes, so we have to convert it into bits, we have to multiply by eight, so that we can count a particular storage size in bits.

So, this was about how our storage is stored in bits.

If I say that one byte is equal to eight bits.

So, if I have to see here, how much is MB.

Which means that how much is megabytes or else if I talk about gigabytes, how much is the GB.

How much is terabyte? So, all these things are related.

If I talk about one terabyte.

Basically, 1000 gigabytes becomes one terabyte, which means 1000 GB of storage.

I talk about 1 GB of storage, even here 1000 MB roughly comes under it.

If I talk about megabytes, even here kilobytes suddenly come into picture.

In 1 MB there are 1000 kilobytes.

So, in this way the order goes down, till the time it doesn’t reach the bits.

Basically, the smallest part of storage we call that as bits.

What are basically bits, in this basically zero and one only can be stored.

As we all know that our computer only understands zeros and ones.

They understand the binary language.

So, whenever we are storing a certain thing that is stored even in zeros and ones but since we club it and tweak it as per the software, which we see actually, that is saved in the background in the formats of zeros and ones.

So, in one bit, one zero gets fitted or one, 1 can fit.

In the same way, we have 8 bits.

So, here we can make 8 different combos of zeros and ones.

So, this was about how the storage space gets divided in bits, bytes and in gigabytes and in terabytes.

This was about the bits, we had also seen that when we talk about data type, our storage is decided by the data type.

Like we had seen till now that our integer data types in that basically, two bytes of storage is allocated.

So, we will see that if we have made one variable whose data type is integer, so the storage of that integer data type’s variable will be allocated in the hard disk, that will be of 2 bytes, which means if we convert it into bits.

So, that becomes 8 multiplied by 2, which means 16 bits.

So, this was about how an integer contains 2 bytes of storage.

Here there is a need to concentrate a little more, we will go in depth with which we will understand that many a times it depends on the processor, how much storage space, we would be allocating to any particular variable.

So, here we saw when we made the integer, integer considered 16 bits of storage, it allocated for the storage.

Here comes the picture of the processor.

What is the processor? It is the part of our CPU or computer, all the processing and calculation work is done by the processor.

We have many types of processors such as , There is one 16 bits processor, in there at a time 16 bits can be processed.

After that there is a 32 bit processor, that is more advanced than a 16 bit one.

In 32 bits, 32 bits can be processed together.

In the same way, a 64 bits processor is available as well.

Where 64 bits are processed together in our computer, in our CPU.

 Based on the bits of the processor, if your processor is of 16 bits, at that time your integer allocates two bytes of storage for self.

Which means it allocates 16 bits for itself.

 But there is something to pay attention on.

If your computer is 64 bits, what happens in 64 bits ?

your integer considers 4 bytes of storage.

So, here based on the processor’s unit, we decide our integer variable or integer type data type, that is basically, how much storage space it would be allocating.

So, if I talk about a 16 bit processor, there my integer will be taking 2 bytes but if I talk about my 64 bits processor, there my integer data type variable that is there, that would be considering 4 bytes of storage.

So, whenever I will be making an integer in my 64 bit’s processor computer.

So, my integer would be considering 4 bytes of storage.

 So how can we know that our computer or processor is of 16 bits or 32 bits or 64 bits, which one is it? It means that our integer variable will be made and in the same way, other variables and other data types that are there, which kind of storage or how much storage it will actually allocate for itself, when we are defining.

All these things we can make with a size of function, we can find out how much storage is our integer data type taking.

How much storage is the float data type taking? In the same way, we can find out the size for other data types.

After that we will be discussing ahead when we would be seeing bit fields practically.

But for now the prerequisite knowledge that is there related to bits, bytes or the information related to the data type storage, that we discussed now.

Now we will straight away go to see what exactly bit fields are now.

And how the bit fields ensure that your storage is efficiently used over here.

So, we all know that “values stored through structures or variables are actually stored in the form of bits.”

So here, we spoke about that whenever we make the variables with the help of structure, union or any other function.

If as per that we are making any variable.

The value that is stored, that is stored in the form of bits in our storage.

If we talk about, bits are allocated in the memory with the basis of data type.

So, what is the data type of the variables based on that depends, how much memory is taken by our bits, how many bits will be accommodated or allocated for that particular variable.

So, this was about how we consider the bits while making a variable through structure or union.

If we go ahead then we will see here that all bits are stored for a value, and are not occupied basically.

“All bits stored for a value are not occupied and they get wasted.”

Here we have to concentrate.

What happens if we have made one integer and If I'm using a 64 bits processor, it will use the four bytes of storage, which means it is 8 multiplied by 4.

It is 64 basically.

So, here the 64 bits of storage will be taken by integer variable.

But it so happens that the integer’s limit is more.

But if I want to store the number 10 in my integer variable and I know that my value that will come inside that variable will not be more than 10, it will be less than then 10..7..8..3..2..1.

This way the values will be there, they won't be more than 10 but to store 10, I will have to make the integer variable.

If I am making the integer variables, so my compiler, my computer will allocate 64 bits storage for my variable but from that if I want to represent 10 digits.

So, 10 will be easily represented in 3 and 4 bits.

And the remaining 60 bits will be wasted for my integer.

So, here you must be understanding that when we convert our integers like 1.. 2.. 3.. 4.. when we convert them in the conditions of zeros and ones.

It depends on that, like we took 1.

We can represent 1 in 3 bits.

If I want to take 50, 50 can be converted into 4 or 5 bits.

 So, in this way smaller the number, we will require smaller bits to represent it.

So, if I'm talking about bits here, I'm talking about zero's and one’s combination over here.

If I want to represent one, how can I do it? I can represent my number 1 with 0, 0, 1.

In the same way, the other numbers that are there, I can convert them into binary format, in bits format I can convert it.

So, here is something to pay attention to.

 If I want to store only 10 numbers and whenever I will use that variable.

My values will not be more than 10.

They will be less than believes that variable value should not be more than 10.

It shouldn't be less than 10.

In this case I can use only three or four bits.

I will use Maximum 4 bits here.

Rest of the 60 bits were allocated with my integer, that will remain empty, that will not come to use.

So, to use those 60 bits.

The thing that we can use the functionality that we can use is called bit fields.

‘Bit fields help us to save these wasted spaces.’

So here, the space that is getting wasted, where we know that in the variable, no more value than this can be stored.

We don't want to store more value than that.

In that situation, in that scenario what we can do is, we can put limit on our variable size with which what happens, the rest of the memory which was supposed to be allocated, that saves from getting allocated for that particular variable and that memory can be used by some other variable, with which our storage is efficiently used.

So, this is how we can use the bit fields in the bit storage, we can efficiently manage our storage, we can use it.

We will go ahead and talk about syntax over here.

Like all the other programs, we have discussed bit fields, but where the bit fields will be written and in which situation it will be used, all these things we will see over here.

So, bit fields basically for the structure or the members in the union, the variables that are there, it is used only for them.

So, if we talk about syntax here.

We have taken a structure for now which we are starting with the ‘struct keyword.’

 In it you can see that we have made two member variables, one is called variable name and the other is called variable name 2, its data type is an unsigned integer.

For both it is an unsigned integer.

Why have we used unsigned? We are not going to store any negative value in it.

If we are not storing the negative values, we write unsigned at the time of variable declaration.

What happens with this? The ability of storing the positive integers increases.

It will happen that if we have to store any negative values, we have to store some space for the negative values as well and some for positive.

And if we don’t have to store any negative values in that same integer variable.

So, the space that was used for the negative, that will add on for positive values and the positive values range will increase.

So, that’s why we are using the unsigned integer data type over here.

You can see here that in front of variable names, we have put the colon and after that, we have written 1.

In the same way, when we made variable name 2, when we declared it.

Even at that time we had our colon before, and wrote 3.

Then we put a semicolon.

With this we basically understand that here 1 and 3 that are respectively written here, these are bit fields.

And what is their work? The way we declared variable names.

After colon whichever number is written, that is basically represented by bits.

So, if I say that in front of variable names, I have written 1.

So, here I want to say that my variable named variable is there.

That will not use the storage of more than 1 bit.

Even though it is of integer data type, even then that will not use more than 1 bit storage.

That I know from the beginning.

As my situation is such, my program is such that I don't want to store any value more than 1 bit in my variable.

 Here I'm talking about the bit, either 0 will be stored or 1 will be stored.

Because in 1 bit either 0 comes or 1.

So, in this way, I will limit the size of my variable name till 1 bit.

What will happen because of it? Rest of the 4 bytes that were supposed to get allocated.

What happened to it?

Rest of my 4 bytes that were about to get allocated for the integer, that entire thing does not get allocated and the rest of the space gets saved here.

So, this was about the bit fields.

On the other side, the other variable that we made, variable name 2, even this has been made in this way.

And here I have written 3 in front of the colon.

So, this means that we are limited here to 3 bits.

What will happen with this? The more storage space that was going to get allocated, as per the integer data type.

That will not be allocated here.

How much ever was required, that we have defined here.

We don’t have to store more than that.

So, that will not be allocated here, with which our storage will be efficiently managed.

So, we learned about the structure, we also saw the members.

So, we will straight away see here, how these bit fields can be practically used.

So, we will quickly go to the practical part and see an example where we use the structure.

There we make bit fields.

And we will see how our storage is efficiently used.

And quickly switch into our text editor and here for the bit fields, we have written a program.

First of all, if we see, we have written a pre-processing command, we have imported the STDIO dot H file.

After that you will see that we have made two structures over here.

The first structure is very normal, in that we have not used any bit field.

Along with that, if you see ahead, the other structure that we have made.

This was our first structure; this is the first structure.

Along with that if we see, this is our second structure.

And in that, we have declared the bit fields.

Here we have written 1 for a and for b we have written 1, similarly.

So, you can see that in the first structure we have not used any bit field.

In the second structure, we have used a bit field.

And the first structure we would be accessing with O 1, all its members.

Along with that, we can access the other structure’s members.

So, what is this program basically? We will see here.

What will this program do? Both my structures that are there here, for those the variable that I had made of struct type o 1 (o one) and o 2.

What will be the exact size of ‘o 1’, that we will be seeing here, with the help of our size of function, which tells the size of the particular variable.

So, here we will see o 1’s size, what is the size that is coming.

Which means combining both these members, how much size we have to actually allocate.

In the same way we will pass o 2 in ‘size of’ and we will see that here we have made use of bit fields, we have used.

Is it effective? Is it able to use our space effectively, we are going to see that here.

So, memory size occupied by o 1, this is what we will print first.

After that in the second line we would be printing, memory size occupied by o 2.

So, you can see that in sizeof, I have first passed o1.

In size of , I have passed o2 here.

So, this is my bit field’s one programme where we are differentiating between our two structures.

In the first structure, we have not used any bit field.

In the second structure, we have made use of the bit field.

We have used it.

Now we will run it over here.

And we will see, how the output they are able to produce here.

So, this is quickly getting executed and run over here.

And you will see in output, the first line that is there, ‘memory size occupied by o1.’

 ‘O 1’ was my structure in which we have not used bit fields, that is representing.

And if you see that, here it has used 8 bytes of storage.

Why has it used it? Because we have made 2 variables here.

Both are of integer type.

So, for A, our compiler 4 bytes storage, even for B, 4 bytes storage was allocated.

And when we added both, o 1 was representing both the variables.

So, after adding it became a total of 8 bytes.

The entire structure that is represented by o 1.

That is allocating 8 bytes of storage.

It is used.

The same thing if we see on the other side.

If we talk about the other structure.

That was represented with o 2, which means you can see here.

This is that structure, here we have made use of bit fields and we have been told that in A variable, more than 1 bit storage will not be occupied there.

Even in b only 1 bit will be used.

In that situation, you will see that here the memory of o 2 is given 4 bytes only.

So, you will see how we were using 8 bytes earlier but after providing bit fields, after using bit fields.

Within the structure, we have reduced the size by 50%.

Earlier 8 bytes of storage were used without the bit fields.

Now the storage used here is only 4 bytes.

And our work is also effectively happening here.

Because we know that we have to use only 1 bit of storage.

So, the other bytes that will be allocated, with the help of our integer data type.

We don’t have to use that.

So, with the help of a bit field, we will eliminate that and we are efficiently able to use our storage here.

So, this was about how we use our bit field.

You can explore it more, you can increase their numbers and see if you have to keep 1, 1 or 5, 6.

In the same way you can change the numbers.

You can change the data types.

And you can explore different types of options with which you will get more information about the bit fields and more experience as well.

So, this was about bit field.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere