CHTP5e - 10-Structures 2
CHTP5e - 10-Structures 2
1
0
C Structures, Unions,
Bit Manipulations and
Enumerations
2
Portability Tip
Bitwise data manipulations are machine dependent
4
Operator Description
& bitwise AND The bits in the result are set to 1 if the corresponding bits
in the two operands are both 1 .
| bitwise inclusive The bits in the result are set to 1 if at least one of the corresponding bits
OR in the two operands is 1.
^ bitwise exclusive The bits in the result are set to 1 if exactly one of the corresponding bits
OR in the two operands is 1.
<< left shift Shifts the bits of the first operand left by the number of bits specified by
the second operand; fill from the right with 0 bits.
>> right shift Shifts the bits of the first operand right by the number of bits specified by
the second operand; the method of filling from the left is machine
dependent.
~ one’s complement All 0 bits are set to 1 and all 1 bits are set to 0.
Bitwise operators.
1 /* Fig. 10.7: fig10_07.c 5
2 Printing an unsigned integer in bits */
3 #include <stdio.h>
4
5 void displayBits( unsigned value ); /* prototype */
6
7 int main( void )
8 {
9 unsigned x; /* variable to hold user input */
10
11 printf( "Enter an unsigned integer: " );
12 scanf( "%u", &x );
13
14 displayBits( x );
15
16 return 0; /* indicates successful termination */
17
18 } /* end main */
19
20 /* display bits of an unsigned integer value */
21 void displayBits( unsigned value )
22 {
23 unsigned c; /* counter */
24
25 /* define displayMask and left shift 31 bits */
26 unsigned displayMask = 1 << 31;
27
28 printf( "%10u = ", value ); displayMask is a 1 followed by 31 zeros
29
30 /* loop through bits */
31 for ( c = 1; c <= 32; c++ ) { Bitwise AND returns nonzero if the leftmost bits
32 putchar( value & displayMask ? '1' : '0' ); of displayMask and value are both 1,
33 value <<= 1; /* shift value left by 1 */ since all other bits in displayMask are 0s.
34
35 if ( c % 8 == 0 ) { /* output space after 8 bits */
36 putchar( ' ' );
37 } /* end if */
38
39 } /* end for */
40
41 putchar( '\n' );
42 } /* end function displayBits */
Results of combining two bits with the bitwise AND operator &.
9
Portability Tip
Right shifting is machine dependent. Right
shifting a signed integer fills the vacated bits with
0s on some machines and with 1s on others.
20
Performance Tip
Bit fields help conserve storage.
1 /* Fig. 10.16: fig10_16.c 26
2 Representing cards with bit fields in a struct */
3
4 #include <stdio.h>
5
6 /* bitCard structure definition with bit fields */
7 struct bitCard {
8 unsigned face : 4; /* 4 bits; 0-15 */
9 unsigned suit : 2; /* 2 bits; 0-3 */
10 unsigned color : 1; /* 1 bit; 0-1 */ Bit fieldsdetermine how much memory
11 }; /* end struct bitCard */
each member of a structure can take up
12
13 typedef struct bitCard Card; /* new type name for struct bitCard */
14
15 void fillDeck( Card * const wDeck ); /* prototype */
16 void deal( const Card * const wDeck ); /* prototype */
17
18 int main( void )
19 {
20 Card deck[ 52 ]; /* create array of Cards */
21
22 fillDeck( deck );
23 deal( deck );
24
25 return 0; /* indicates successful termination */
26
27 } /* end main */
28
29 /* initialize Cards */ 27
30 void fillDeck( Card * const wDeck )
31 {
32 int i; /* counter */
33
34 /* loop through wDeck */
35 for ( i = 0; i <= 51; i++ ) {
36 wDeck[ i ].face = i % 13;
37 wDeck[ i ].suit = i / 13;
38 wDeck[ i ].color = i / 26;
39 } /* end for */
40
41 } /* end function fillDeck */
42
43 /* output cards in two column format; cards 0-25 subscripted with
44 k1 (column 1); cards 26-51 subscripted k2 (column 2) */
45 void deal( const Card * const wDeck )
46 {
47 int k1; /* subscripts 0-25 */
48 int k2; /* subscripts 26-51 */
49
50 /* loop through wDeck */
51 for ( k1 = 0, k2 = k1 + 26; k1 <= 25; k1++, k2++ ) {
52 printf( "Card:%3d Suit:%2d Color:%2d ",
53 wDeck[ k1 ].face, wDeck[ k1 ].suit, wDeck[ k1 ].color );
54 printf( "Card:%3d Suit:%2d Color:%2d\n",
55 wDeck[ k2 ].face, wDeck[ k2 ].suit, wDeck[ k2 ].color );
56 } /* end for */
57
58 } /* end function deal */
Card: 0 Suit: 0 Color: 0 Card: 0 Suit: 2 Color: 1
Card: 1 Suit: 0 Color: 0 Card: 1 Suit: 2 Color: 1
Card: 2 Suit: 0 Color: 0 Card: 2 Suit: 2 Color: 1
Card: 3 Suit: 0 Color: 0 Card: 3 Suit: 2 Color: 1
Card: 4 Suit: 0 Color: 0 Card: 4 Suit: 2 Color: 1
Card: 5 Suit: 0 Color: 0 Card: 5 Suit: 2 Color: 1
Card: 6 Suit: 0 Color: 0 Card: 6 Suit: 2 Color: 1
Card: 7 Suit: 0 Color: 0 Card: 7 Suit: 2 Color: 1
Card: 8 Suit: 0 Color: 0 Card: 8 Suit: 2 Color: 1
Card: 9 Suit: 0 Color: 0 Card: 9 Suit: 2 Color: 1
Card: 10 Suit: 0 Color: 0 Card: 10 Suit: 2 Color: 1
Card: 11 Suit: 0 Color: 0 Card: 11 Suit: 2 Color: 1
Card: 12 Suit: 0 Color: 0 Card: 12 Suit: 2 Color: 1
Card: 0 Suit: 1 Color: 0 Card: 0 Suit: 3 Color: 1
Card: 1 Suit: 1 Color: 0 Card: 1 Suit: 3 Color: 1
Card: 2 Suit: 1 Color: 0 Card: 2 Suit: 3 Color: 1
Card: 3 Suit: 1 Color: 0 Card: 3 Suit: 3 Color: 1
Card: 4 Suit: 1 Color: 0 Card: 4 Suit: 3 Color: 1
Card: 5 Suit: 1 Color: 0 Card: 5 Suit: 3 Color: 1
Card: 6 Suit: 1 Color: 0 Card: 6 Suit: 3 Color: 1
Card: 7 Suit: 1 Color: 0 Card: 7 Suit: 3 Color: 1
Card: 8 Suit: 1 Color: 0 Card: 8 Suit: 3 Color: 1
Card: 9 Suit: 1 Color: 0 Card: 9 Suit: 3 Color: 1
Card: 10 Suit: 1 Color: 0 Card: 10 Suit: 3 Color: 1
Card: 11 Suit: 1 Color: 0 Card: 11 Suit: 3 Color: 1
Card: 12 Suit: 1 Color: 0 Card: 12 Suit: 3 Color: 1
29
Portability Tip
Bit-field manipulations are machine dependent.
For example, some computers allow bit fields to
cross word boundaries, whereas others do not.
Performance Tip
Although bit fields save space, using them can
cause the compiler to generate slower-executing
machine-language code. This occurs because it
takes extra machine language operations to
access only portions of an addressable storage
unit. This is one of many examples of the kinds
of space–time trade-offs that occur in computer
science.
31
1 January
2 February
3 March Like symbolic constants, enumeration constants
4 April
5 May
are replaced by their values at compile time
6 June
7 July
8 August
9 September
10 October
11 November
12 December
34