This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

placement of cast within expressions

 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I got some funny results of a cast used within an expression - maybe someone can explain why this happens...
int oneIntVariable = ( (int)myLongVariable % anotherIntVariable ) ;
The above line of code resulted in strange values for (int)myLongVariable and, consequently, oneIntVariable.
I got the desired cast to work only when I placed it outside the expression, like this:
int oneIntVariable = (int)( myLongVariable % anotherIntVariable ) ;
Adding inner parentheses like this
int myVariable = ( ( (int)myLongVariable ) % myIntVariable ) ;
got me the same strange values for (int)myLongVariable and myVariable.
Any hints? Thanks once again,
Pauline

 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you cast a long to an int, it just chops off the front of the number, it doesn't round it off. For example, if you cast the binary long
0000 0000 0000 0000 0000 0000 1111 0000 0000 0000 0000 1111 0000 0000 0000 0000
to an int, the compiler assumes that you know what you are doing because you are using an explicit cast and chops off the first 32 bits, leaving you with
0000 0000 0000 1111 0000 0000 0000 0000

If you try to assign a long to an int without explicitly casting it, the compiler will generate a warning.

This can get really weird sometimes. If the number has 1's in certain spots, you could end up with a negative number.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it was weird alright. Didn't get any compiler errors, but when I tested for the actual value of the (int)myLongVariable, it was getting big negative values. But this happened only when the cast was placed directly next to it and not outside the expression parens.
I still don't understand why the one way works and the other two don't. Dunh...
Does it have something to do with precedence?
Of course, the basic problem is "...the compiler assumes you know what you are doing..."
Ha!

 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is that you have to get the number small enough to fit into an int before you do the cast. Isn't it amazing how powerful parens can be.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! thanks, Marilyn.
hmmm, powerful parens - how fitting for mother's day
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic