By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,965 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Question About Casting

P: n/a
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];
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Nov 14 '05 #2

P: n/a
Thomas Stegen <ts*****@cis.strath.ac.uk> wrote in
news:3f********@nntphost.cis.strath.ac.uk:
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.
Nov 14 '05 #3

P: n/a
David Vestal <so****************@mailcity.com> wrote in
news:Xn**********************************@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 ->
--
Nov 14 '05 #4

P: n/a
In <Xn**********************************@130.133.1. 4> David Vestal <so****************@mailcity.com> 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]) << 8)
+ (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: Da*****@ifh.de
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.