Bit field in C
Suppose your C program includes a number of TRUE / FALSE variables grouped in a structure called a page to check if the manufactured goods have sufficient width and height allowed, as follows:
struct { unsigned int chieurong ; unsigned int chieucao ; } trangthai ;
This structure requires 8 bytes of memory but in fact it reserves 0 or 1 bytes per variable. C programming language has a way to optimize memory in this case. You are using variables within the structure then you can define the size of the variables, it will notify the C compiler that only the number of bytes is used. For example, the above structure can be rewritten as follows:
struct { unsigned int chieurong : 1 ; unsigned int chieucao : 1 ; } trangthai ;
Now the above structure will require 4 bytes of memory for the page variable, but only 2 bits are used to store the value. You must use up to 32 variables with this length of 1 bit, so this structure will use 4 bytes and when you have 33 variables, it will allocate the next value in memory and start using 8 bytes. Now let's examine the example below to understand this definition.
#include #include /* dinh nghia mot cau truc don gian */ struct { unsigned int chieurong ; unsigned int chieucao ; } trangthai1 ; /* dinh nghia mot cau truc voi cac truong bit */ struct { unsigned int chieurong : 1 ; unsigned int chieucao : 1 ; } trangthai2 ; int main ( ) { printf ( "Bo nho bi chiem giu boi trangthai1 la: %dn" , sizeof ( trangthai1 )); printf ( "Bo nho bi chiem giu boi trangthai2 la: %dn" , sizeof ( trangthai2 )); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compiling and running the above C program will result:
Declare Bit Field in C
Declare a Bit Field inside a structure with the following pattern:
struct { kieu_du_lieu [ ten_thanh_vien ] : do_rong ; };
Below is a description for the variable elements in a Bit Field:
ElementDescriptionkieu_du_lieu An integer type can determine how the Bit field is interpreted. This type can be int, signed int, unsigned int ten_thanh_vien Name of Bit Field. do_rong (is the width) The number of bits contained in a field. The length must be less than or equal to the Bit Field length of a specific object.A variable defined with a given magnitude value is called a Bit field . A Bit Field can store more than one single bit eg you need a variable to store values from 0 to 7, then you can define the Bit Field with a maximum length of 3 bits as follows:
struct { unsigned int tuoi : 3 ; } Tuoi ;
The above definition will instruct the C compiler that the variable will use 3 bits to store values, if you use more than 3 bits it will not allow you to do so. Now try the example below:
#include #include struct { unsigned int tuoi : 3 ; } Tuoi ; int main ( ) { Tuoi . tuoi = 3 ; printf ( "Bo nho bi chiem giu boi Tuoi la Sizeof( Tuoi ) = %dn" , sizeof ( Tuoi ) ); printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); Tuoi . tuoi = 6 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); Tuoi . tuoi = 7 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); /* Bay gio chung ta thu in nhieu hon 3 bit */ printf ( "n-----------------------n" ); Tuoi . tuoi = 8 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); Tuoi . tuoi = 9 ; printf ( "Tuoi.tuoi : %dn" , Tuoi . tuoi ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous post: Union in C
Next lesson: Keyword typedef in C
You should read it
- Against the 'invisible enemy' Covid-19, the US military could also engage in battle by aircraft and ships.
- The Earth's magnetic field is about to reverse the polarity?
- How to Program
- 10 harsh facts about success in IT
- Do you know what programming language is?
- Top 5 programming languages to develop AI
- A unique 7000-year-old figurine was discovered in a field
- Successfully developing an AI model that can predict a seizure with an accuracy of 99.6%
May be interested
- Successfully developing an AI model that can predict a seizure with an accuracy of 99.6%predicting the seizure itself before it happens can be considered a feat in the medical field, and more importantly, this success stems from an ai system.
- How to Become a Computer Security Consultantinformation security experts protect organizations against malware, hackers, employee mistakes, and natural disasters. breaking into this field requires you to possess hands-on experience, extensive training, and management prowess. you'll...
- Some tricks in Windows Scriptingwhether you are a person working in the it field, such as a web designer, or an office worker, even a student, needs windows scripting because it will give you a lot. utilities.
- What is NFC? How does NFC work?nfc is a mainstream wireless technology, thanks to the development of online payment systems like samsung pay and google pay, especially when it comes to high-end devices and even more mid-range options.
- TOP Generative AI tools and applications by fieldfrom content creation to gaming, genai tools are revolutionizing processes, reducing manual effort and opening doors to limitless possibilities. here are the top generative ai tools and applications.
- How to Become a Gender Studies Professora gender studies professor conducts research and teaches at the college level about gender and feminism in society. this is a highly interdisciplinary field, meaning it draws from many other field of study such as history, political...
- Benefits from BCC function when sending emailbcc is abbreviated for blind carbon copy, allowing you to hide the list of recipients in emails. unlike addressing in the to: field or cc: (carbon copy: co-send), set the address in the bcc field: make other recipients not see the mail address.
- Apple is acquiring start-up with 10 years of experience in the VR field for $ 100 millionthis will be a quality addition for apple to continue to pursue its ambition with the field of virtual reality (vr).
- 10 harsh facts about success in ITthere are facts that are not pleasant and hard to overcome - something that anyone who works in this field must accept and adapt to survive. here are 10 harsh facts about success in it. invite you to read!
- How to Design and Implement a Field Using C Sharpthe first step in designing a class usually is to define its fields. fields are the backbone that classes rely upon, and so, the process of designing and implementing fields is crucial to the overall process of designing classes. this...