Assuming you have two 32-bit DWORD x and y, how can you detect if their sum would result in an overflow, but without resorting to native assembly to examine the carry flag. I'd like something with arithmetic or binary operators. I figured there might be some bit testing methods to figure it out. Thanks
Detecting 32-bit dword + dword carry / C++
1.4k Views Asked by oldSkool At
2
There are 2 best solutions below
14
Mario
On
Should pretty much save to assume, tell me if I missed any case (this won't work in compilers adhering to C++98 or newer standards):
int overflowSum(DWORD a, DWORD b) {
return (b > 0) ? (a + b < a) : (a + b > a);
}
If you consider your DWORD to be unsigned, you can simplify it:
int overflowSum(DWORD a, DWROD b) {
return a + b < a;
}
Related Questions in C++
- C++ using std::vector across boundaries
- Linked list without struct
- Connecting Signal QML to C++ (Qt5)
- how to get the reference of struct soap inherited in C++ Proxy/Service class
- Why we can't assign value to pointer
- Conversion of objects in c++
- shared_ptr: "is not a type" error
- C++ template using pointer and non pointer arguments in a QVector
- C++ SFML 2.2 vectors
- Lifetime of temporary objects
- I want to be able to use 4 different variables in a select statement in c ++
- segmentation fault: 11, extracting data in vector
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- How can I print all the values in this linked list inside a hash table?
- Configured TTL for A record(s) backing CNAME records
Related Questions in C
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in OVERFLOW
- How to make nested div scrollable when parent is too small on x axis?
- Python RuntimeWarning overflow encountered in double_scalars
- Bootstrap Thumbnail Cropping and Positioning
- Div overflowing
- Inner Div overflow issue?
- Allow absolutely positioned child to render outside parent with overflow: hidden
- Overflow visible on text input, is it possible?
- VBA Override Error 6 Overflow (Create Infinite Loop)?
- HTML & CSS - Absolute positioned div overflows relative div, unexpected result
- HTML / CSS : hide overflow of fixed height div in pdf print
- Ensure vertical content overflows horizontally
- Overflow-y and Fixed Positioned Bug Fix in Internet Explorer
- Setting overflow: scroll on a table with display: flex
- Bug in Chromium with rendering css transformation transition
- position sticky polyfill with overflow support
Related Questions in ADDITION
- I'm trying to make the "merge function" work by writing a Callback.
- Add comment to PowerPoint by macro
- VB.net: How to make original variable value fulfill 2 statements?
- VB.net: How to add different multipliers together?
- Add up numbers in files
- Why is this floating point addition result not correct?
- Adding a button dynamically with JQuery without using .append
- SQL Invalid ALTER TABLE option
- trying to delete automatically added and sorted answers from a VBA textbox into excel cells
- adding an unknown number of numbers in java
- sqlite add x days to date (x stored in table)
- how do you find the summation of elements in a set?
- dbo.aspnet_Users table is empty where it shouldn't be
- Multiple NULL addition into a List in Java
- Adding tags to mp3 with eyed3 results in no change
Related Questions in DWORD
- C# DWORD and QWORD - signed and unsigned madness
- query about few threading terms
- Conversion from size_t to DWORD, possible loss of data
- Warning - Conversion from size_t to DWORD, possible loss of data
- Unexpected results when casting dword to byte [4] (endianity swap?)
- use of the DWORD_PTR
- Detecting 32-bit dword + dword carry / C++
- C: 4 elements of an array to a DWORD
- C++ Pointer as DWORD
- Hex byte values to DWORD (ascii) format
- In C++, how can I use wstringstream to combine/concat wstring + NULL + DWORD
- MFC: How to i convert DWORD and BYTE to LPCTSTR in order to display in MessageBox
- Low and High DWORD => size_t
- Reading a DWORD Date From Registry Using Python & _winreg
- C++/WinAPI - Removing a flag from a DWORD (bitwise operator?)
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Why not test the sum?