delorie.com/archives/browse.cgi | search |
From: | "Tim Nicholson" <T DOT J DOT Nicholson AT btinternet DOT com> |
Newsgroups: | comp.os.msdos.djgpp |
Subject: | Re: Bit field query |
Date: | Thu, 4 Oct 2001 21:21:44 +0100 |
Organization: | Skyforce avionics Limited |
Lines: | 79 |
Message-ID: | <9pigh8$abp$1@uranium.btinternet.com> |
References: | <9pidst$kgu$1 AT epos DOT tesco DOT net> |
NNTP-Posting-Host: | host213-1-61-232.btinternet.com |
X-Priority: | 3 |
X-MSMail-Priority: | Normal |
X-Newsreader: | Microsoft Outlook Express 5.50.4133.2400 |
X-MimeOLE: | Produced By Microsoft MimeOLE V5.50.4133.2400 |
To: | djgpp AT delorie DOT com |
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
Reply-To: | djgpp AT delorie DOT com |
B0 is the LSB and apears in the leftmost bit of the 32 bit field. The following program will prove this. Tim //Compile this to show the bit field. struct bit_field { unsigned int b0 : 1; unsigned int b1 : 1; unsigned int b2 : 1; unsigned int b3 : 1; unsigned int b4 : 1; unsigned int b5 : 1; unsigned int b6 : 1; unsigned int b7 : 1; }; int main(void) { struct bit_field my_bits; unsigned int *my_word; short int i; my_word = (unsigned int *) &my_bits; *my_word = 0; my_bits.b0 = 1; my_bits.b5 = 1; for (i=0; i<32;i++) { printf("%d", (*my_word) & 0x80000000 ? 1:0); *my_word <<= 1; } printf("\n"); } "Graham Warren" <Moosehead AT tesco DOT net> wrote in message news:9pidst$kgu$1 AT epos DOT tesco DOT net... > I have a question about bit-fields. > > In a program I have defined a bit-field using the code shown below. > > > /* define bit-field */ > > struct { > unsigned int b0 : 1; > unsigned int b1 : 1; > unsigned int b2 : 1; > unsigned int b3 : 1; > unsigned int b4 : 1; > unsigned int b5 : 1; > unsigned int b6 : 1; > unsigned int b7 : 1; > } bit_field; > > /* */ > > > > My questions are: would bit_field.b0 be the furthest left or furthest right > bit if the byte is written out 00000000? > Would this change if more bits were defined in the bit field to make it > spill over into a larger than 1 byte area? > > Also which bit in 00000000 is referred to as the most-significant and which > is the least-significant? > > > Thankyou > Graham Warren > >
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |