PLZZZZZZ need help on bit fileds

This is a discussion on PLZZZZZZ need help on bit fileds within the C Programming forums, part of the General Programming Boards category; here is the structures for date, time and attribute suppose that there are the date( 2 bytes ), time( 2 ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    9

    PLZZZZZZ need help on bit fileds

    here is the structures for date, time and attribute
    suppose that there are the date( 2 bytes ), time( 2 bytes ) and attribute( 1 byte ) stored somewhere inside array of buffer.
    here is the attribute:
    Flags in the Attribute byte
    7 6 5 4 3 2 1 0
    Reserved A D V S H R 0000h

    here is the time:
    The field is in the special format described below:

    Time Format
    15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
    Hours (0-23) Minutes (0-59) Seconds (0-29) 0000h
    Seconds are counted with 2 seconds interval, so a value of 29 in this field gives 58 seconds.

    here is the date:
    The field is in the special format described below:

    Date Format
    15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
    Years from 1980 Month of year (1-12) Day of month (1-31) 0000h

    any one know how to use bit fileds?

    my question is how do i get the date( 2 bytes ), time( 2 bytes ) and attribute( 1 byte ), because they have different length of bits
    i mean each data that i want in these three structures.

    or is there any othere way to these three things

    PLzzz help me out

    if u need more info, plz post here.


    Code:
    typedef struct                
    {
    	unsigned	two_secs : 5;	/* low 5 bits: 2-second increments */
    	unsigned	minutes : 6;	/* middle 6 bits: minutes */
    	unsigned	hours : 5;		/* high 5 bits: hours (0-23) */
    }DosTime;				/* 2 bytes total */
    
    typedef struct             
    {
    	unsigned	date : 5;		/* low 5 bits: date (1-31) */
    	unsigned	month : 4;		/* middle 4 bits: month (1-12) */
    	unsigned	year : 7;		/* high 7 bits: year - 1980 */
    }DosDate;				/* 2 bytes total */
    
    typedef struct                 
    {
    	unsigned	int	read_only : 1;	/* b0 */
    	unsigned	int	hidden : 1;
    	unsigned	int	system : 1;
    	unsigned	int	volume_label : 1;
    	unsigned	int	directory : 1;
    	unsigned	int	archive : 1;
    	unsigned	int	reserved : 2;	/* b6, b7 */
    }Attrib;

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    The time and date fields use the following "packed" storage format:

    Packed Time:

    15:11 - > Hour
    10:5 - > Minute
    4:0 - > Second / 2


    Packed Date:

    15:9 - > Year íV 1980
    8:5 - > Month
    4:0 - > Day

    Attribute:
    7:6 - > Reserved
    5 - > A
    4 - > D
    3 - > V
    2 - > S
    1 - > H
    0 - > R

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Not sure what you want

    DosDate now;

    now.date = 4;
    now.month = 7;
    now.year = 2002 - 1980;

    The notation for accessing them is exactly the same as for normal structure members


    > stored somewhere inside array of buffer.
    Perhaps
    memcpy ( &now, &buff[pos], sizeof(now) );

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    It's very unclear what you are looking for! From what I've understood, you can go about this way:
    Code:
    struct _buffer
    {
                short int s_date;    /*   2 Bytes of DATE   */
                short int s_time;    /*   2 Bytes of TIME   */
                char        attr;    /*   1 Byte of Attribute   */
    }buffer;
    
    ...
    
    fn()
    {
               date_1_bit  = buffer.s_date & (1 << 1);
               date_7_bit  = buffer.s_date & (1 << 7);
    
               time_2_bit  = buffer.s_time & (1 << 2);
               time_15_bit = buffer.s_time & (1 << 15);
    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >any one know how to use bit fileds?

    A bitfield is a struct where each member consists of 1 or more bits. This is useful for a lot of cases. For example if you want to access the individual bits of a byte easily without having to deal with bitshifting, bitmasks and that kind of techniques.

    Code:
    /* This is an example of how to define a bitfield which can be used to access bits of a byte individually. */
    typedef 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;
    } byte_bitfield;
    
    /* This function demonstrates how to access the individual bits. */
    void function ()
    {
        byte_bitfield byte;
    
        /* set first bit of byte to 0 */
        byte.b0 = 0;
        /* set second bit of byte to 1 */
        byte.b1 = 1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21