Question about Data Types and Shifting?

Discussion in 'C Programming' started by salsipius, Jan 20, 2006.

  1. salsipius

    salsipius Guest

    Can someone please help me clarify the below code. I think the shifting
    has to do with converting datatypes and/or loss of data but am not
    really clear on the details, could you help shed some light please?


    // Allocate array
    for( i = 0; i < Length; i++ )
    {

    //pArray_00 is a BYTE Array [2408*2048] -- Here a cast is used because
    the WORD variable is comming from a VB INT(2 Bytes) but the pArray_00
    is an array of type BTYE(1 byte)

    pArray_00[ i ] = ( BYTE )( pWord_00[ i ] >> 4 );
    }
     
    salsipius, Jan 20, 2006
    #1
    1. Advertising

  2. salsipius wrote:
    > Can someone please help me clarify the below code. I think the shifting
    > has to do with converting datatypes and/or loss of data but am not
    > really clear on the details, could you help shed some light please?
    >
    >
    > // Allocate array
    > for( i = 0; i < Length; i++ )
    > {
    >
    > //pArray_00 is a BYTE Array [2408*2048] -- Here a cast is used because
    > the WORD variable is comming from a VB INT(2 Bytes) but the pArray_00
    > is an array of type BTYE(1 byte)
    >
    > pArray_00[ i ] = ( BYTE )( pWord_00[ i ] >> 4 );
    > }
    >


    What is the size of byte (and hence BYTE) on your system? Hint:
    grep for CHAR_BIT macro definition.

    Looking at your code it may well be 12, as you're shifting your
    data left by 4 bits (think integer division by 16 here). Or,
    your byte is shorter but (you hope) your data, once divided by
    16, will fit. The latter is a dangerous assumption, at least for
    the accuracy, since you shoehorn it to BYTE anyway, potentially
    hiding overflows.

    I'm sure others will pick on different things (including my
    musings).

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
     
    Vladimir S. Oka, Jan 20, 2006
    #2
    1. Advertising

  3. Vladimir S. Oka wrote:
    > salsipius wrote:
    >> Can someone please help me clarify the below code. I think the shifting
    >> has to do with converting datatypes and/or loss of data but am not
    >> really clear on the details, could you help shed some light please?
    >>
    >>
    >> // Allocate array
    >> for( i = 0; i < Length; i++ )
    >> {
    >>
    >> //pArray_00 is a BYTE Array [2408*2048] -- Here a cast is used because
    >> the WORD variable is comming from a VB INT(2 Bytes) but the pArray_00
    >> is an array of type BTYE(1 byte)
    >>
    >> pArray_00[ i ] = ( BYTE )( pWord_00[ i ] >> 4 );
    >> }
    >>

    >
    > What is the size of byte (and hence BYTE) on your system? Hint: grep for
    > CHAR_BIT macro definition.
    >
    > Looking at your code it may well be 12, as you're shifting your data
    > left by 4 bits (think integer division by 16 here). Or, your byte is


    Well, I assumed here WORD = 2 BYTEs = 16 bits, but we don't know
    that, do we? My mistake...

    > shorter but (you hope) your data, once divided by 16, will fit. The
    > latter is a dangerous assumption, at least for the accuracy, since you
    > shoehorn it to BYTE anyway, potentially hiding overflows.


    Shifting by 4 we get (2*CHAR_BIT - 4) bits out of pWord_00
    which is guaranteed to be larger than a BYTE, unless a BYTE is 4
    bits, which would make the implementation non-conformant
    (standard requires CHAR_BIT >= 8).

    Hence, you _always_ lose at least some high-order bits of your
    array.

    > I'm sure others will pick on different things (including my musings).


    And I'm sure this still stands as well. ;-)

    Cheers

    Vladimir

    --
    My e-mail address is real, and I read it.
     
    Vladimir S. Oka, Jan 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    457
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    480
  3. ramu
    Replies:
    2
    Views:
    346
    rlblaster
    Feb 20, 2006
  4. Gary Roach
    Replies:
    0
    Views:
    125
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    138
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page