467,179 Members | 1,179 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,179 developers. It's quick & easy.

comparision of 32 bit signed numbers

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
  • viewed: 1651
Share:
2 Replies
crazy4perl
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
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.

Similar topics

17 posts views Thread by Christopher Dyken | last post: by
9 posts views Thread by dam_fool_2003@yahoo.com | last post: by
10 posts views Thread by tinesan@gmail.com | last post: by
64 posts views Thread by ng5000@gmail.com | last post: by
27 posts views Thread by REH | last post: by
11 posts views Thread by Frederick Gotham | last post: by
1 post views Thread by nicola | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.