Calc Mode 2: Two’s Complement

Emacs provides a method for working out numbers in two’s complement form

M-x calc        Turn on calc mode
O d2            Turns on two's complement binary
b w 8           Sets the word length to 8 bits
d z             Displays leading zeroes

Now you can enter numbers. Don’t forget to use underscore for negative numbers

_3 (-3) gives 2##11111101

You can press y to yank the result back into the last buffer.

Quick Conversions

if you want to quickly convert between binary, denary and hex you can use quick calc mode:
To convert 17 from Denary to Binary

C-x * q         Enter quick calc mode
17              Hit enter
Result: 17 =>  17  (16#11, 8#21, 2#10001, "")

To convert 1100 from Binary to Denary (or Hex)

C-x * q         Enter quick calc mode
2#1100          Enter in minibuffer.  Use 16# to enter a Hex number
Result: 12 =>  12  (16#C, 8#14, 2#1100, "")

See Also

Calc Mode 0: Basics

Having seen it written on a blackboard in the Simpsons, I wanted to check if

398712 + 436512 = 447212

thus disproving Fermat’s Last Theorem. My pocket calculator confirmed the expression to be true, but those are big numbers, and the calculator lacks precision.

This seemed like a perfect opportunity to use calc mode.

M-x calc        to enter calc mode

First enter the left hand side

3987
<Enter>
12 ^
4365
<Enter>
12 ^

Both numbers are now there in the stack:

--- Emacs Calculator Mode ---
2:  16134474609751291283496491970515151715346481
1:  47842181739947321332739738982639336181640625

Hit + to add them together. Now to enter the right hand side.

4472
<Enter>
12 ^

Now the left and right sides of the original expression are there in the stack, and you can see that whilst they may be sufficiently equal to fool a pocket calculator, they can’t fool Emacs.

--- Emacs Calculator Mode ---
2:  63976656349698612616236230953154487896987106
1:  63976656348486725806862358322168575784124416

Hit  to see the difference:

1211886809373872630985912112862690

d g to group the number, then hit y to yank the number back into the current buffer.

1,211,886,809,373,872,630,985,912,112,862,690

… as I just did there.

See Also

Calc Mode 1: Binary Numbers