473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Question About Casting

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
4 1529
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Vijay Kumar R Zanvar | last post by:
Hi, Which section of C99 says that return value of malloc(3) should not be casted? Thanks. -- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
26
by: Method Man | last post by:
Say I have the following: int main(void) { char* p, q; p = (char*) malloc(sizeof(char)*10); q = (p + 100) - 99; /* legal? */ free(q - 1); /* legal? */ .... return 0; }
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
14
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.