Advertisement
BenTibnam

Two's Complement Negation

Oct 31st, 2023
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.26 KB | Software | 0 0
  1. let toNegative = function(n)
  2. {
  3.     // take the binary complement
  4.     n = ~n;
  5.  
  6.     // add 1
  7.     n += 1;
  8.  
  9.     // return final result of -n
  10.     return n
  11. }
  12.  
  13. let a = parseInt(prompt("Enter an integer"));
  14. let na = toNegative(a);
  15.  
  16. console.log(`n: ${a}`);
  17. console.log(`n: ${na}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement