Amazon.com Widgets Bit Fields--Page 282
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 17, 2014, 06:38:06 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 13 - Underlying C Language Features
| | | |-+ Bit Fields--Page 282
Pages: [1] Go Down
Print
Author Topic: Bit Fields--Page 282 (Read 1754 times)
jainsanket
Newbie
*
Posts: 13






on: November 14, 2010, 05:34:59 PM

I have been reading the book in order. Can someone expand on the following ideas mentioned on page 282?

1) "This should not present a problem unless you are dealing with data that a different program or a different machine created"

2) "Bit Fields are packed into units as they appear in the structure definition, where the size of a unit is defined by the implementation and is most likely a word. The Objective-C compiler does not rearrange the bit field definitions to try to optimize storage space"

I am not sure how important this is but this is one of the very few roadblocks I had while reading this book.

Sanket
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: November 14, 2010, 07:40:45 PM

It'a not that important.  It has to do with packing memory more efficiently.  For example, you can represent a boolean values yes/no using a single bit of the computer's memory.  Remember, 8 bits in a bye, and 8 bytes is a 64-bit word.  That means, you could represent 64 boolean values in a single 64-bit word.   That's just one example.  Bitfields, as described in the chapter provide the syntactic means to "pack" smaller-sized date together for efficiency.  

For example, here's part of the header from the  UITabelViewCell class.  That class handles the formatting of each cell presented in a table on an iOS device.  Here in the header file a structure called _tableCellFlags is defined and, as you can see, bitfields are used extensively:

Code: (Objective-C)
    struct {
        unsigned int showingDeleteConfirmation:1;
        unsigned int separatorStyle:3;
        unsigned int selectionStyle:3;
        unsigned int selectionFadeFraction:11; // used to indicate selection
        unsigned int editing:1;
        unsigned int editingStyle:3;
        unsigned int accessoryType:3;
        unsigned int editingAccessoryType:3;
        unsigned int showsAccessoryWhenEditing:1;
        unsigned int showsReorderControl:1;
        unsigned int showDisclosure:1;
        unsigned int showTopSeparator:1;
        unsigned int disclosureClickable:1;
        unsigned int disclosureStyle:1;
        unsigned int showingRemoveControl:1;
        unsigned int sectionLocation:3;
        unsigned int tableViewStyle:1;
        unsigned int shouldIndentWhileEditing:1;
        unsigned int fontSet:1;
        unsigned int usingDefaultSelectedBackgroundView:1;
        unsigned int wasSwiped:1;
        unsigned int highlighted:1;
        unsigned int separatorDirty:1;
        unsigned int drawn:1;
        unsigned int drawingDisabled:1;
        unsigned int style:12;
        unsigned int showingMenu:1;
        unsigned int clipsContents:1;
        unsigned int animatingSelection:1;
        unsigned int allowsIsolatedLayout:1;
        unsigned int backgroundColorSet:1;
    } _tableCellFlags;

Here you see lots of single-bit flags (the :1 that's tacked on means to use a single-bit of storage).   A bit is either on or off, that means a single bit can represent YES or NO. Notice that editingStyle, for example, is 3 bits (:3).  That means it can store a value from 0 - 7.

Cheers,

Steve
Last Edit: November 14, 2010, 07:44:54 PM by skochan Logged
jainsanket
Newbie
*
Posts: 13






Reply #2 on: November 16, 2010, 01:14:35 PM

Thanks Steve for your continued response.

So, this is how I understood. By unit, you mean the cumulative memory space allocated to every bitfield in the structure definition. Also, the memory allocated is in the same order (left to right or vice versa) as that specified in the structure definition. Objective-C compiler does not rearrange the memory allocated to individual bitfield in order to optimize storage space. But why do you say the size of the unit is mostly word or 64 bit. For example, if have any 2 boolean flags, the size of each unit would be 2 bits.
Kindly correct me here if something is incorrect.

Also, how data stored in left to right or vice versa can be an issue on different machine or program?

Sanket
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.