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

+4

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 https://code.sololearn.com/cUt1P0Z08Hxn/?ref=app

4/14/2019 5:25:20 PM

michal

2 Answers

New Answer

+2

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

+6

There are a series of posts of structure packing from C++ Soldier (Babak) user that explains this https://www.sololearn.com/post/78037/?ref=app