# Thread: Mathematical Carrying and Borrowing?

1. ## Mathematical Carrying and Borrowing?

I've been assigned the job of emulating a CPU, and one of the registers is the carry and borrow flag. During addition and subtraction, if carrying or borrowing occurs, the register is set to 1 (true). If no carrying or borrowing occurs, the register is set to 0 (false).

My question is this: is there an easy way in C or C++ to determine if carrying or borrowing occurs during addition and subtraction?

2. Of course there is! Just look at your number base, for carrying. If the immediate result of adding two digits is greater than or equal to your number base, then you have a carry.

For borrowing, (subtraction), if the immediate result of a subtraction of two digits results in anything less than 0, then a borrow is necessary.

3. Originally Posted by MattMik
I've been assigned the job of emulating a CPU, and one of the registers is the carry and borrow flag. During addition and subtraction, if carrying or borrowing occurs, the register is set to 1 (true). If no carrying or borrowing occurs, the register is set to 0 (false).

My question is this: is there an easy way in C or C++ to determine if carrying or borrowing occurs during addition and subtraction?
A typical bit adder has two outputs: the value and a carry. So to determine if a carry occurs you simply look at the carry of the MSB operation.

For subtraction you need to determine/specify which method you are using to represent negative numbers.

4. Was addition ever demonstrated to you on paper, or was this just glossed over at school and you were just told to press the '+' key on the calculator?

5. Originally Posted by Salem
Was addition ever demonstrated to you on paper, or was this just glossed over at school and you were just told to press the '+' key on the calculator?
What's a paper?