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

Carry detection

Hi.
I have a question concering carry handling in C++.

When adding some value to a 32 bit Integer which causes an overflow how
is it possible to check, if a carry bit would have been set.

I know how it would word in assebmler, but I do not find a solution for C++.

thanks in advance, Hipo
Jun 11 '06 #1
3 8147
"Hipo" writes:
I have a question concering carry handling in C++.

When adding some value to a 32 bit Integer which causes an overflow how is
it possible to check, if a carry bit would have been set.

I know how it would word in assebmler, but I do not find a solution for
C++.


Alas, it is not to be. Most, if not all, high-level languages ignore integer
overflow detection. The language mavens simply throw the information out
with the garbage. You can find lots of posts on what to do about that on
Usenet. I suggest you take a look at the archives for comp.lang.c There
are no solutions that I don't consider pretty applying.
Jun 11 '06 #2
Hipo wrote:
When adding some value to a 32 bit Integer which causes an overflow how
is it possible to check, if a carry bit would have been set.


This is much easier in C++ than in Java since you at least have unsigned
arithmetic. If A and B are unsigned and A+B carries, then A+B < A (also
< B). Unfortunately you have to check at each step so it can get pretty
ugly.

If you have the luxury of a larger size integer then you can promote
each value to the larger size, do a bunch of work, and take the high
bits as carry when you store the low bits back to the smaller size.
Jun 11 '06 #3
Robert Mabee <rm****@comcast.net> wrote:
Hipo wrote:
When adding some value to a 32 bit Integer which causes an overflow how
is it possible to check, if a carry bit would have been set.

This is much easier in C++ than in Java since you at least have
unsigned arithmetic. If A and B are unsigned and A+B carries,
then A+B < A (also
B). Unfortunately you have to check at each step so it can
get pretty ugly.


This might work:

int a, b , c;
c = a + b;
int carry_happened = 1 & ((a & b & ~c | ~a & ~b & c) >> 31);

Steve
Jun 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
6
by: Gustav Medler | last post by:
Hello, there is a known problem with Opera and the execution of content shown in <NOSCRIPT> tag. Everythings works fine, if there is only one simple script like:...
18
by: Mickey Segal | last post by:
On comp.lang.java.programmer we are discussing problems created for Java programs by pop-up blockers (in the thread "showDocument blocked by new microsoft pop-up blocker"). Our problem is that...
8
by: R. Smits | last post by:
I've have got this script, the only thing I want to be changed is the first part. It has to detect IE version 6 instead of just "Microsoft Internet Explorer". Can somebody help me out? I tried...
0
by: Bryan Parkoff | last post by:
I break one U_WORD variable into two U_BYTE variables. I prefer to manipulate two U_BYTE variables instead of one U_WORD variable using Carry. Please look at my example using U_WORD variable...
7
by: mosaic | last post by:
Hi, all I really interested in how to check the memory leak of a program. Your smart guys, do you have excellent ideas that could share with me? Thank you. The following is my idea: In C...
0
by: darrenhello | last post by:
hi there, I am doing my last year's project and I have a 'little' problem. I have to do an edge detection filter. for now, some normal edge detection filters that I used worked fine but there a...
0
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
10
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.