what is bit fields in structure | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

what is bit fields in structure

Hi Can anyone help me to understand importance of bit fields? What does it mean and how it is useful?

2nd Jun 2020, 1:33 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Answers
+ 3
A structure or a C++ class can contain bit fields that allow you to access individual bits. You can use bit fields for data that requires just a few bits of storage. A bit field declaration contains a type specifier followed by an optional declarator, a colon, a constant expression, and a semicolon.
2nd Jun 2020, 1:55 PM
💞 SHAINA 💞
💞 SHAINA 💞 - avatar
+ 2
Thanks all for this... I read it but any example would be of great help
2nd Jun 2020, 4:21 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
3rd Jun 2020, 6:27 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Bit fields are important for two main reasons: 1.Allows you to manipulate individual bits using a field name.(just as AMOGHA. A. K. has said) 2.unnamed bit fields are used to align the data in structs in a machine dependent way
11th Jun 2020, 12:35 PM
Anthony Maina
Anthony Maina - avatar
+ 1
Finally come up with use case when bit field can be applied to save memory https://code.sololearn.com/c7L891KF9TXI/?ref=app Thanks all of you for your time and help
15th Jun 2020, 8:39 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
But does this help in structure padding ? If not, what purpose it helps ? And accessing memory is not in our hand.. Isn't it?
11th Jun 2020, 4:18 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
My structure padding code is below: https://code.sololearn.com/c4na2St2ZDgQ/?ref=app Does bit can be useful here or not? How
11th Jun 2020, 4:19 PM
Ketan Lalcheta
Ketan Lalcheta - avatar