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.
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)
MadanReddy said:
1 decade ago
Hai,sathuragiri.. i think,this explanation will be helpful for u..
union Point
{
unsigned int x:4;
unsigned int y:4;
int res;
};
In general unsigned integer limit is represented with 2bytes(16bits).
consider a simple example,
104 binary equivalent is(1101000)
Here only 7bits are used to store 104. remaining 9bits will be unused bits.
These unused bits will occupy extra space on memory.this can be avoided by using "bit field" concept.
union Point
{
unsigned int x:4;
unsigned int y:4;
int res;
};
In general unsigned integer limit is represented with 2bytes(16bits).
consider a simple example,
104 binary equivalent is(1101000)
Here only 7bits are used to store 104. remaining 9bits will be unused bits.
These unused bits will occupy extra space on memory.this can be avoided by using "bit field" concept.
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)
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).
Soumya Roy said:
1 decade ago
Yes of course, bit fields are allowed in Unions. But read the question carefully, its given bit field are not allowed in unioin.
So that sentence is FALSE. Therefore the answer given here is correct and as per your text books. So just chill.
So that sentence is FALSE. Therefore the answer given here is correct and as per your text books. So just chill.
Mukesh Kumar said:
5 years ago
Bit field used for save memory for that member who occupied 1 or 2 bit rather then int.
Sumanth said:
1 decade ago
I have read in a text book that bit fields are allowed in unions.
Khushnood said:
1 decade ago
Yes yes, I also read in book that bit are allowed in unoin.
Kaustubh said:
1 decade ago
Assigning of values to a memory location is bit field?
Prabhu said:
1 decade ago
Yes, I also read that bitfields are allowed in union.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers