473,395 Members | 2,446 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,395 software developers and data experts.

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
2 1755
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

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

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
4
by: BigMan | last post by:
What is the difference between char and signed char, short and unsigned short, int and signed int, long and unsigned long?
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
64
by: ng5000 | last post by:
Hi, What's the point of a signed char? As I see it a char represents a character (not an integer, use an int type e.g. short int if you want an 8 bit number, or one of the new types, uint8 I...
27
by: REH | last post by:
I asked this on c.l.c++, but they suggested you folks may be better able to answer. Basically, I am trying to write code to detect overflows in signed integer math. I am trying to make it as...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
1
by: nicola | last post by:
Hi, I'm a bit confused about the following: #include <iostream> int main (int argc, char** argv) { std::cout << "Bits of unsigned char: " << std::numeric_limits<unsigned char>::digits <<...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.