bit field in c interview questions

For example, we can store age from 0 to 16 and student id from 0 to 256.

Since we have limited the number of values to lesser value, we cannot store any value greater than this. That means, if we try to store age as 17 or more, it will not allow us to store them. It will consider them as zero.

In above student structure size of the structure without bit field is size of (StdId) + size of (Age) = 8 bytes + 8 Bytes = 16 bytes. After using bit fields to its members, it is 8 bits + 4 bits = 12 bits = 1.5 bytes which is very much less. Hence we can save lot of memory.

Please note that we cannot use sizeof operator to find the size of variable whose size is in bits. Please note above the difference between the structures with and without bitfields. Also see that how the member elements display the value based on their bits. Here Age can store values from 0 to 15 as its bit field size is 4. Hence it displays the values correctly till it has value 15. Above this number it shows the remainder values when tried to display.

The output comes out to be negative. What happened behind is that the value 31 was stored in 5 bit signed integer which is equal to 11111. The MSB is a 1, so it’s a negative number and you need to calculate the 2’s complement of the binary number to get its actual value which is what is done internally. By calculating 2’s complement you will arrive at the value 00001 which is equivalent to decimal number 1 and since it was a negative number you get a -1. A similar thing happens to 12 in which case you get 4-bit representation as 1100 which on calculating 2’s complement you get the value of -4.

In C, we can specify size (in bits) of structure and union members. The idea is to use memory efficiently when we know that the value of a field or group of fields will never exceed a limit or is within a small range. For example, consider the following declaration of date without the use of bit fields.

C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf

a) Any type b) Nothing c) Integer value d) Nonnegative integer value View Answer Answer: d Explanation: None.

4. For what minimum value of x in a 32-bit Linux OS would make the size of s equal to 8 bytes?

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers. «

9. Bit fields can only be declared as part of a structure. a) false b) true c) Nothing d) Varies View Answer Answer: b Explanation: None.

8. Which of the following is not allowed? a) Arrays of bit fields b) Pointers to bit fields c) Functions returning bit fields d) None of the mentioned View Answer Answer: d Explanation: None.

Syntax of bit fields in C:

In C language declaration of the bit-field structure or union is similar to the declaration of the normal structure or union, the main difference is that bit-field member is declared with a specified number of bits preceded by the colon.

struct{ type-specifier declarator opt : constant-expression};

In the above declaration, constant-expression specifies the width of the field in bits and must be a non-negative integer value. If the value is zero, the declaration has no declarator.

The type-specifier for the declarator must be _Bool, signed int, unsigned int, or some other implementation-defined type. It is implementation-defined whether atomic types are permitted.

Let’s take an example to understand the structure bit field.

In the above example, ‘structure packed_data’ contains 7 members. In which five-member (data1 to data5) has 1 bit and 6th and 7th member has the 3 and 6 bits.


What is the use of bit field in C?

In C, we can specify size (in bits) of structure and union members. The idea is to use memory efficiently when we know that the value of a field or group of fields will never exceed a limit or is within a small range.

How bit fields are stored in memory?

Again, storage of bit fields in memory is done with a byte-by-byte, rather than bit-by-bit, transfer.

What are the questions asked in C interview?

C Interview Questions

What are the basic Datatypes supported in C Programming Language? What do you mean by Dangling Pointer Variable in C Programming? What do you mean by the Scope of the variable? What is the scope of the variables in C?

Can we use bit field in union?

Bit fields CANNOT be used in union.

Related Posts

Leave a Reply

Your email address will not be published.