Professional Documents
Culture Documents
Week 8 - Unions and Bitfields
Week 8 - Unions and Bitfields
Week 8 - Unions and Bitfields
union number {
◦ int x;
◦ double y;
};
Union Example
union data • Important: The compiler does not test if
{ the data is being read in the correct
int idata ; format.
float fdata ; union data d; d.idata=10; float f=d.fdata; /∗
will give junk∗/
6 char ∗sdata ;
}
d1 . i d a t a =10;
d1 . f d a t a =3.14F ;
d1 . sdata="hello world" ;
‘(x>>y)‘ is equivalent to dividing x with 2^y or shifting the bits y units to the right
Bitwise Operators
Useful in programming for operations such as
logical indexing used to subset data
member of a struct or union. Known as bit fields, these enable better memory utilization
by storing data in the minimum number of bits required. Bit field members