Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Checking overflow in C

Status
Not open for further replies.

mahaju

Full Member level 2
Joined
Mar 17, 2007
Messages
125
Helped
7
Reputation
14
Reaction score
2
Trophy points
1,298
Activity points
2,252
Hi
let we have
int a,b,c; // may be char or float, anything actually
c=a+b;

let int type be represented by 4 bytes. Let's say a+b requires 1 bit more than 4 bytes (ie, let's say the result is 1 00....0 (32 zeroes, in binary)). This would result in C=0, and I am sure the computer's microprocessor would set some kind of overflow flag. Is there any built in method to check this in C?

I am actually working on building a number type that is 1024 bits long (for example, int is a built in number type that is 32 bits long). I have attempted this using unsigned char type arrays with 128 elements. I also need to define addition and subtraction operations on these numbers. I have written the code for addition but I am having problem on subtraction. I don't need to worry about getting negative results because the way I will call the subtracting function always ensures that the result of subtraction is always positive, but to implement the subtraction function I need to somehow get the 2's complement of the subtrahend, which is it self my custom 1024 bit number.

I am sorry if it is difficult to understand my description. If needed I will elaborate it more. I am including my code for the adding function and the incomplete subtracting function. the NUM_OF_WORDS is a constant declared as

#define NUM_OF_WORDS 128

Please let me know if you did not understand my question or any part of my code
 

Attachments

  • code.zip
    832 bytes · Views: 34

Do you mean something like checking Overflow flag??? If so no there is no direct way for checking that. But you can always write one.

for more details you can refer to C standards( I did but could not find any)
 

like I said I need to build custom data type of 1024 bits
I did this by storing my number in a character array of 128 elements (total 128*8 bytes)
to add then I added the corresponding elements of two arrays LSW(least significant word) first
So, if adding array element number 0 creates an overflow, array element number 1 should be incremented
The addition routine I have implemented, but this fact is causing problems in writing a subtraction routine, where I am having trouble implementing borrowing from a higher word
It seems I will need to convert the subtrahend into it's two's complement, but it would be easier to do if I could find a way to detect the internal overflow flag being set

---------- Post added at 06:34 ---------- Previous post was at 06:34 ----------

what I really need to implement is this
https://www.edaboard.com/threads/208878/

Please help me if you have any ideas
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top