checkFloats

checkFloats - } // if borrow else // not borrow { if((num1...

This preview shows pages 1–2. Sign up to view the full content.

// Author Sean Davis #include "bitops.h" short add(short num1, short num2) { short i, result = 0, carry = 0; for(i = 1; i <= 0x100; i = i << 1) if(carry) { result = result | i; else // both bits not set { result = result | i; carry = 0; } // if both bits are zero. } // if carry set else // carry not set carry = 1; else // not both num1 and num2 result = result | i; return result; } // add() short subtract(short num1, short num2) { short i, result = 0, borrow = 0; for(i = 1; i <= 0x100; i <<= 1) if(borrow) { borrow = 0; else // not (num1 bit == 1 and num2 bit == 0) result = result | i; else // num1 bit == 0 result = result | i;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } // if borrow else // not borrow { if((num1 & i) && ((num2 & i) == 0)) result = result | i; else // not (num1 bit == 1 and num2 bit == 0) if(((num1 & i) == 0) && (num2 & i)) { result = result | i; borrow = 1; } // if } // else no borrow return result; } // subtract() short multiply(short num1, short num2) { short i, result = 0; for(i = 1; i < 0x100; i <<= 1) { if(num2 & 1) result = result + num1; num1 = num1 << 1; num2 = num2 >> 1; } // for each bit return result; } // multiply() short divide(short num1, short num2) { short i, result = 0; num2 = num2 << 8; for(i = 0x100; i > 0; i = i >> 1) { if(num1 >= num2) { result = result | i; num1 = num1 - num2; } // if num2 = num2 >> 1; } // for each bit return result; } // divide()...
View Full Document

This note was uploaded on 02/21/2010 for the course ECS 40175 taught by Professor Amenta during the Fall '09 term at UC Davis.

Page1 / 2

checkFloats - } // if borrow else // not borrow { if((num1...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online