C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - True / False Questions (Q.No. 3)
3.
Bit fields CANNOT be used in union.
Answer: Option
Explanation:
The following is the example program to explain "using bit fields inside an union".
#include<stdio.h>
union Point
{
unsigned int x:4;
unsigned int y:4;
int res;
};
int main()
{
union Point pt;
pt.x = 2;
pt.y = 3;
pt.res = pt.y;
printf("\n The value of res = %d" , pt.res);
return 0;
}
// Output: The value of res = 3
Discussion:
20 comments Page 1 of 2.
Mukesh Kumar said:
5 years ago
Bit field used for save memory for that member who occupied 1 or 2 bit rather then int.
Javqui said:
7 years ago
The questions is CAN USE?,
The answer is YES, and can be very useful if used with a struct.
To be more specific, a struct of bitfields inside a union is a nice way to have two or more interpretations of the same data. It is very useful in low-level programming with hardware.
union Point{
struct {
unsigned int x:4;
unsigned int y:4;
};
int res;
};
int main(){
union Point pt;
pt.x = 2;
pt.y = 3;
printf("\n The value of res = %d %d %d" , pt.res, pt.x, pt.y );
pt.res = pt.y;
printf("\n The value of res = %d %d %d" , pt.res, pt.x, pt.y );
return 0;
}
The answer is YES, and can be very useful if used with a struct.
To be more specific, a struct of bitfields inside a union is a nice way to have two or more interpretations of the same data. It is very useful in low-level programming with hardware.
union Point{
struct {
unsigned int x:4;
unsigned int y:4;
};
int res;
};
int main(){
union Point pt;
pt.x = 2;
pt.y = 3;
printf("\n The value of res = %d %d %d" , pt.res, pt.x, pt.y );
pt.res = pt.y;
printf("\n The value of res = %d %d %d" , pt.res, pt.x, pt.y );
return 0;
}
(1)
Ishwarya said:
7 years ago
What is bit field?
Saltwind said:
7 years ago
What is meant by bitfields?
Pranali said:
8 years ago
In union, which datatype has highest size is equal to that union size.
ex. union demo
{
int i;
char c;
};
sizeof(demo);// 4(bcoz size of int is greater than size of char)
the addition of all data elements or data members is equal to the size of the structure.
ex.
struct demo
{
int i;
char c;
};
sizeof(demo);// 5(addition of int and char).
ex. union demo
{
int i;
char c;
};
sizeof(demo);// 4(bcoz size of int is greater than size of char)
the addition of all data elements or data members is equal to the size of the structure.
ex.
struct demo
{
int i;
char c;
};
sizeof(demo);// 5(addition of int and char).
Sankari said:
9 years ago
Give any two difference between union and structure.
Rijo said:
9 years ago
What is a bit field?
Kaustubh said:
1 decade ago
Assigning of values to a memory location is bit field?
Kavin said:
1 decade ago
Please explain bit field?
Researcher said:
1 decade ago
struct
{
type [member_name] : width ;
};
Below the description of variable elements of a bit field:
Type An integer type that determines how the bit-field's value is interpreted. The type may be int, signed int, unsigned int.
Member_name The name of the bit-field.
Width The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type.
The variables defined with a predefined width are called bit fields.
{
type [member_name] : width ;
};
Below the description of variable elements of a bit field:
Type An integer type that determines how the bit-field's value is interpreted. The type may be int, signed int, unsigned int.
Member_name The name of the bit-field.
Width The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type.
The variables defined with a predefined width are called bit fields.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers