Mathematical Carrying and Borrowing?

This is a discussion on Mathematical Carrying and Borrowing? within the C Programming forums, part of the General Programming Boards category; I've been assigned the job of emulating a CPU, and one of the registers is the carry and borrow flag. ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by MattMik View Post
    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. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Quote Originally Posted by Salem View Post
    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?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21