Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. int fromTwoComp(int input)
  2. {
  3. int mask = 1 << 31; // but 1 in the first bit
  4. int neg = (input & mask);
  5.  
  6. if(neg == mask) //if first bit of neg is 1 then input is negative
  7. {
  8. input -= 1;
  9. input = 1 - input; //swap 0 and 1
  10. input = input | mask; //make first bit 1
  11. return input;
  12. }
  13. else //not negative, its just like normal binary
  14. {
  15. return input;
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement