|
-
March 21st, 2009, 08:45 PM
#5
Re: Why does this loop turn out negatives?
Signed numbers (e.g. long, int, short, char) take the most significant bit (MSb) as the sign indicator. When the MSb is 0, it is considered positive and when it is 1 (binary 1), it is considered negative.
In your case, your number became 0x80000000 (hexadecimal value) and this makes your number to be considered negative. The actual value can be computed by getting the two’s compliment on the 31 remaining bits.
Hope it will help you.
Edit: You could use unsigned number (unsigned int for example) so you can use the whole 32 bit value. For your 100 loops, you might need a variable type that can handle 100 bits.
Last edited by rxbagain; March 21st, 2009 at 08:54 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|