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

comparision of 32 bit signed numbers

P: 19
Hello friends,
I need help in comparision of 32 bit signed numbers.
I am generating random signed numbers using some logic & when i am doing addition of two numbers i am getting maximum value 0X80000000.
how to avoid this?
Example:#
Expand|Select|Wrap|Line Numbers
  1. use strict ;
  2.  
  3. my $a;
  4. my $b;
  5. my $result;
  6.  
  7. $range = 2**31; # 2^(n-1): n=32
  8.  $min = -2**31; #lowest integer
  9.  $val= int(rand($range)) + $min;
  10.  print "$val\n"
  11.  
  12.   $a = $val;
  13.   $b = $val;
  14.   $result = $a+$b;
  15.  
In the above example, $result will overflow i.e exceeds the range 0x8000000 after addition of two signed numbers, & am getting result as 0x8000000 only, in order to avoid this what i need to do?


Thanks
Raghavendra
Oct 31 '08 #1
Share this Question
Share on Google+
2 Replies


crazy4perl
P: 20
Hello friends,
I need help in comparision of 32 bit signed numbers.
I am generating random signed numbers using some logic & when i am doing addition of two numbers i am getting maximum value 0X80000000.
how to avoid this?
Example:#
Expand|Select|Wrap|Line Numbers
  1. use strict ;
  2.  
  3. my $a;
  4. my $b;
  5. my $result;
  6.  
  7. $range = 2**31; # 2^(n-1): n=32
  8.  $min = -2**31; #lowest integer
  9.  $val= int(rand($range)) + $min;
  10.  print "$val\n"
  11.  
  12.   $a = $val;
  13.   $b = $val;
  14.   $result = $a+$b;
  15.  
In the above example, $result will overflow i.e exceeds the range 0x8000000 after addition of two signed numbers, & am getting result as 0x8000000 only, in order to avoid this what i need to do?


Thanks
Raghavendra
I dont know but its working fine for me

val := -1149894656
result := -2299789312
Oct 31 '08 #2

P: 19
hey man i have just posted an example in order understand my question,
if overflows what to do ? & if it in hexadecimal format how to compare with >0 or <0, i am getting the wrong result
Oct 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.