Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about Data Types and Shifting?

Reply
Thread Tools

Question about Data Types and Shifting?

 
 
salsipius
Guest
Posts: n/a
 
      01-20-2006
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 );
}

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-20-2006
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.
 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-20-2006
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[i]
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 >= .

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Argument type of function and safe types and types, arguments canhandle safely ittium C++ 4 12-09-2011 11:05 AM
Questions about memory structure of value types and reference types Sam Kong Javascript 12 10-17-2006 07:26 PM
equivalent c data types for vc++ data types ramu C Programming 2 02-20-2006 09:33 AM
Disparity between XML Schema types and .Net CLS types MikeL ASP .Net Web Services 2 02-22-2005 06:08 AM
Data type question for OLEdb data types Brian Henry ASP .Net 1 10-05-2003 03:18 PM



Advertisments