CS 192 - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

CS 192

Description:

How many values can we store in 4 and 2 bits? 2n values (0 thru 2n-1) in n bits. Conserves memory: how many things storable in 6 bits or 1 byte? Only a character ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 8
Provided by: Sham4
Category:
Tags: are | bits | byte | how | in | many

less

Transcript and Presenter's Notes

Title: CS 192


1
CS 192
  • Lecture 19
  • Winter 2003
  • February 09-10, 2004
  • Dr. Shafay Shamail

2
Unions
  • Look just like structures and have same syntax
  • Use union keyword instead of struct
  • Like a struct, can hold different data types, BUT
    only one type at a time i.e. members share
    storage
  • Like Sayeed Saigol (auditorium) can hold
    different classes, but only one at a time
  • Also, Sayeed Saigols size is equal to the
    largest class size
  • But better than building a different room for
    each class saves space
  • Likewise, a union is automatically allocated
    memory as large as the largest data type it
    contains as its member
  • Saves memory

3
Unions
  • include ltiostream.hgt
  • union int_or_double
  • int x
  • double y
  • void main()
  • int_or_double number/only 8 bytes for a
    double allocated/
  • number.x 100/store as int/
  • cout ltlt number.xltltendlltltnumber.yltltendl
  • number.y 100.0 /store as double. int value
    lost/
  • coutltltnumber.xltltendlltltnumber.yltltendl
  • Output 100 -9.25596e061 0 100
    //2nd and 3rd values are junk
  • This output is system dependent. System tries to
    read the number at the memory location in the
    format you apply. Succeeds for the correct type,
    junk for the wrong type
  • Try with int x and float y. Same storage space
    required for both, but still garbage values.
  • Programmer is responsible for storing and
    interpreting properly

4
Unions
  • include ltiostream.hgt
  • union int_or_char
  • int x
  • char y
  • void main()
  • int_or_char value
  • value.x 97//store as int
  • cout ltlt value.xltltendlltltvalue.yltltendl
  • value.y 'b'//store as char
  • coutltltvalue.xltltendlltltvalue.yltltendl
  • Output 97 a 98 b
  • Can do all the things that we can do with a
    struct
  • Union can have a struct as a member, and vice
    versa

5
Unions
  • First initializer stored as the data type of the
    first member
  • include ltiostream.hgt
  • union int_or_double
  • int x
  • double y
  • void main()
  • int_or_double number
  • number.x 100.3
  • cout ltlt number.xltltendlltltnumber.yltltendl
  • number.y 100
  • coutltltnumber.xltltendlltltnumber.yltltendl
  • Output 100 junk junk 100
  • Apart from memory, unions also useful where you
    know a value will be either one of two types e.g.
    you can have a union for items that have either a
    char name20 or an int serial_no

6
Bit Fields
  • An int or unsigned member of a structure or union
    can be declared to consist of a specified no. of
    bits
  • Such a member is called a bit field, and the no.
    of associated bits is called its width
  • struct card
  • unsigned pips 4//cant do so outside
    struct/union
  • unsigned suit 2
  • How many values can we store in 4 and 2 bits?
  • 2n values (0 thru 2n-1) in n bits
  • Conserves memory how many things storable in 6
    bits or 1 byte? Only a character
  • Here, 64 1-bit variables using bit fields
  • The width is at most the no. of bits in a machine
    word

7
  • An example that uses bit fields for cards and
    outputs them. Also note how array of struct
    passed as usual array
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • struct BitCard
  • unsigned face 4
  • unsigned suit 2
  • void fillDeck(BitCard )
  • void deal(BitCard )
  • main()
  • BitCard deck52
  • fillDeck(deck)
  • deal(deck)
  • return 0
  • void fillDeck(BitCard wDeck)
  • for (int i 0 i lt 51 i)
  • wDecki.face i 13
  • wDecki.suit i / 13
  • / Output cards in two column format. Cards 0
    25 subscripted with k1 in column 1. Cards 26-51
    subscripted with k2 in column 2 /
  • void deal(BitCard wDeck)
  • for (int k1 0, k2 k1 26 k1 lt 25
    k1, k2)
  • cout ltlt "Card" ltlt setw(3) ltlt
    wDeckk1.face ltlt " Suit"ltlt setw(2) ltlt
    wDeckk1.suit ltlt " "
  • cout ltlt "Card" ltlt setw(3) ltlt
    wDeckk2.face ltlt " Suit"ltlt setw(2) ltlt
    wDeckk2.suit ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com