Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question About Casting

Reply
Thread Tools

Question About Casting

 
 
David Vestal
Guest
Posts: n/a
 
      12-11-2003
I'm reading a 24-bit address from a char* named i_msg, and I expected these
two snippets to be equivalent. They aren't; the first doesn't work. What
gives?

SNIPPET 1:

unsigned long address;
address = ((unsigned long) i_msg[2]) << 16
+ ((unsigned short) i_msg[3]) << 8
+ i_msg[4];

SNIPPET 2:

unsigned long address;
address = i_msg[2];
address <<= 8;
address += i_msg[3];
address <<= 8;
address += i_msg[4];
 
Reply With Quote
 
 
 
 
Thomas Stegen
Guest
Posts: n/a
 
      12-11-2003
David Vestal wrote:

> I'm reading a 24-bit address from a char* named i_msg, and I expected these
> two snippets to be equivalent. They aren't; the first doesn't work. What
> gives?
>
> SNIPPET 1:
>
> unsigned long address;
> address = ((unsigned long) i_msg[2]) << 16
> + ((unsigned short) i_msg[3]) << 8
> + i_msg[4];
>


The addition is done before the shifting.

--
Thomas.

 
Reply With Quote
 
 
 
 
David Vestal
Guest
Posts: n/a
 
      12-11-2003
Thomas Stegen <(E-Mail Removed)> wrote in
news:3fd8a7db$(E-Mail Removed):

> David Vestal wrote:
>
>> I'm reading a 24-bit address from a char* named i_msg, and I expected
>> these two snippets to be equivalent. They aren't; the first doesn't
>> work. What gives?
>>
>> SNIPPET 1:
>>
>> unsigned long address;
>> address = ((unsigned long) i_msg[2]) << 16
>> + ((unsigned short) i_msg[3]) << 8
>> + i_msg[4];
>>

>
> The addition is done before the shifting.
>


*groan* Thanks. I'm too old to be making mistakes like that.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      12-11-2003
In <Xns944E7B4ABF634someoggetridofthisma@130.133.1. 4> David Vestal <(E-Mail Removed)> writes:

>I'm reading a 24-bit address from a char* named i_msg, and I expected these
>two snippets to be equivalent. They aren't; the first doesn't work. What
>gives?


You have to throw more parentheses at the problem: the compiler is
blissfully ignoring your nice formatting of the expression and uses the
C precedence rules instead

>SNIPPET 1:
>
>unsigned long address;
>address = ((unsigned long) i_msg[2]) << 16
> + ((unsigned short) i_msg[3]) << 8
> + i_msg[4];


This is interpreted as:

(((unsigned long) i_msg[2]) << (16 + ((unsigned short) i_msg[3]))) <<
(8 + i_msg[4])

which probably not what you want. Try the following instead:

address = (((unsigned long) i_msg[2]) << 16)
+ (((unsigned short) i_msg[3]) <<
+ (unsigned char) i_msg[4];

Note that, unless plain char is unsigned, the last cast I have added is
needed, too.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      12-11-2003
David Vestal <(E-Mail Removed)> wrote in
news:Xns944E7ED546523someoggetridofthisma@130.133. 1.4:

>>> I'm reading a 24-bit address from a char* named i_msg, and I expected
>>> these two snippets to be equivalent. They aren't; the first doesn't
>>> work. What gives?
>>>
>>> SNIPPET 1:
>>>
>>> unsigned long address;
>>> address = ((unsigned long) i_msg[2]) << 16
>>> + ((unsigned short) i_msg[3]) << 8
>>> + i_msg[4];
>>>

>>
>> The addition is done before the shifting.
>>

>
> *groan* Thanks. I'm too old to be making mistakes like that.


Using | instead of + would have worked. Were you thinking of that?

--
- Mark ->
--
 
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
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
Casting Performance and Pattern Question Remco ASP .Net 1 04-11-2004 05:21 AM
casting question David Eynon Java 2 07-01-2003 06:13 AM



Advertisments