Bits and sizeof structure | SoloLearn: Learn to code for FREE!


Bits and sizeof structure

I got something similar in the challenges. Could you please explain me how it works and why is the output as it is? Thanks

4/14/2019 5:25:20 PM


2 Answers

New Answer


michal Bit fields (int a :1) are useful for saving on memory. It also provide direct access to individual bits without explicit bit extraction. The logic is if all you need is individual bits then why waste memory by defining integers. size of bit is 4 because it has one field of type int. And sizeof int is 4 bytes bit2 size is 4 instead of 8 because two bits easily fits into 1 integer, hence compiler will not allocate extra int


There are a series of posts of structure packing from C++ Soldier (Babak) user that explains this