Difference between revisions of "Bitfields programming"
From Teknologisk videncenter
m |
m (→C and C++ programming) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=C and C++ programming= | =C and C++ programming= | ||
− | + | In the example below - Alarmclock data structure - the complete structure will live in 4 bytes on a 32 bit memorysystem. | |
<source lang=c> | <source lang=c> | ||
#include <stdio.h> | #include <stdio.h> | ||
− | |||
struct display { | struct display { | ||
− | unsigned format : 1; // 24 hour or 12 hour format | + | unsigned format : 1; // 24 hour or 12 hour format ( 0 = 24 and 1 = 12) |
unsigned pm : 1; // PM and AM leds. If 12 hour format pm or am (pm=0 is am) | unsigned pm : 1; // PM and AM leds. If 12 hour format pm or am (pm=0 is am) | ||
− | unsigned alarm : 1; // Alarm on/off LED. | + | unsigned alarm : 1; // Alarm on/off LED. |
− | unsigned digit1 : | + | unsigned digit1 : 2; // Hour MSD 7 segment ( 0 to 2 ) |
− | unsigned digit2 : 4; // Hour LSD 7 segment | + | unsigned digit2 : 4; // Hour LSD 7 segment ( 0 to 9 ) |
− | unsigned digit3 : 4; // Minute MSD 7 segment | + | unsigned digit3 : 3; // Minute MSD 7 segment ( 0 to 5 ) |
− | unsigned | + | unsigned digit4 : 4; // minute LSD 7 segment ( 0 to 9 ) |
+ | unsigned alarm1 : 2; // Alarm Hour MSD 7 segment ( 0 to 2 ) | ||
+ | unsigned alarm2 : 4; // Alarm Hour LSD 7 segment ( 0 to 9 ) | ||
+ | unsigned alarm3 : 3; // Alarm Minute MSD 7 segment ( 0 to 5 ) | ||
+ | unsigned alarm4 : 4; // Alarm minute LSD 7 segment ( 0 to 9 ) | ||
}; | }; | ||
+ | |||
+ | void main(void) { | ||
+ | struct display d; | ||
+ | printf("Structure size: %i\n", (int) sizeof d); | ||
+ | } | ||
</source> | </source> | ||
+ | |||
=Links= | =Links= | ||
*[http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03defbitf.htm IBM on C bitfields] | *[http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03defbitf.htm IBM on C bitfields] | ||
*[http://tdistler.com/2008/07/21/cc-using-bitfields-effectively Using Bitfields Effectively] (volatile example) | *[http://tdistler.com/2008/07/21/cc-using-bitfields-effectively Using Bitfields Effectively] (volatile example) | ||
+ | |||
+ | [[Category:C]] | ||
+ | [[Category:C++]] |
Latest revision as of 10:46, 11 February 2013
C and C++ programming
In the example below - Alarmclock data structure - the complete structure will live in 4 bytes on a 32 bit memorysystem.
#include <stdio.h>
struct display {
unsigned format : 1; // 24 hour or 12 hour format ( 0 = 24 and 1 = 12)
unsigned pm : 1; // PM and AM leds. If 12 hour format pm or am (pm=0 is am)
unsigned alarm : 1; // Alarm on/off LED.
unsigned digit1 : 2; // Hour MSD 7 segment ( 0 to 2 )
unsigned digit2 : 4; // Hour LSD 7 segment ( 0 to 9 )
unsigned digit3 : 3; // Minute MSD 7 segment ( 0 to 5 )
unsigned digit4 : 4; // minute LSD 7 segment ( 0 to 9 )
unsigned alarm1 : 2; // Alarm Hour MSD 7 segment ( 0 to 2 )
unsigned alarm2 : 4; // Alarm Hour LSD 7 segment ( 0 to 9 )
unsigned alarm3 : 3; // Alarm Minute MSD 7 segment ( 0 to 5 )
unsigned alarm4 : 4; // Alarm minute LSD 7 segment ( 0 to 9 )
};
void main(void) {
struct display d;
printf("Structure size: %i\n", (int) sizeof d);
}
Links
- IBM on C bitfields
- Using Bitfields Effectively (volatile example)