Question about Data Types and Shifting?

S

salsipius

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 );
}
 
V

Vladimir S. Oka

salsipius said:
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
 
V

Vladimir S. Oka

Vladimir said:
salsipius said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top