Programming Search⌘K

Conceptadvanced

Ones' complement and sign-magnitude

Also called ones' complement, ones complement, 1's complement, sign-magnitude, sign and magnitude, signed magnitude, negative zero, end-around carry, Internet checksum.

Sign-magnitude and ones' complement are two older ways to store signed integers. Each has a negative zero and a symmetric range, and both left the C and C++ standards for two's complement.

Applies to
core-languages

Idea

A signed integer type must show the sign of a value in its bits. Wikipedia lists "sign–magnitude, ones' complement, two's complement, and offset binary" as "The four best-known methods"1. All of them store a value that is 0 or more in the usual binary form, and they differ for negative values:

  • Sign-magnitude. The top bit is the sign, and the other bits are the absolute value. Wikipedia gives the example of 43, which is 00101011 in 8 bits, and -43, which is 101010111.
  • Ones' complement. "In such a system, a number is negated (converted from positive to negative or vice versa) by computing its ones' complement."2 The ones' complement of a value is the value with each bit inverted.
BITS = 8


def sign_magnitude(n):
    return (0x80 if n < 0 else 0) | abs(n)


def ones_complement(n):
    return n if n >= 0 else ~(-n) & 0xFF


def twos_complement(n):
    return n & 0xFF


print("value  sign-magnitude  ones' complement  two's complement")
for n in (43, -43, 0, 127, -127, -128):
    sm = f"{sign_magnitude(n):08b}" if -127 <= n else "none"
    oc = f"{ones_complement(n):08b}" if -127 <= n else "none"
    tc = f"{twos_complement(n):08b}"
    print(f"{n:5}  {sm:>14}  {oc:>16}  {tc:>16}")
print("negative zero:", f"{0x80:08b}", f"{0xFF:08b}")
Output
value  sign-magnitude  ones' complement  two's complement
   43        00101011          00101011          00101011
  -43        10101011          11010100          11010101
    0        00000000          00000000          00000000
  127        01111111          01111111          01111111
 -127        11111111          10000000          10000001
 -128            none              none          10000000
negative zero: 10000000 11111111

Both older forms have two zeros: 00000000 and a negative zero. In 8 bits, the negative zero is 10000000 in sign-magnitude and 11111111 in ones' complement. Both forms also have a symmetric range, -127 to 127 in 8 bits, so -128 has no form. Two's complement has one zero and the range -128 to 127.

How it works

Sign-magnitude. The form is like a number written with a minus sign. "This approach is directly comparable to the common way of showing a sign (placing a "+" or "−" next to the number's magnitude)."1 Negation inverts only the top bit. Addition is harder: "Addition and subtraction require different behavior depending on the sign bit"1. For example, the sum 5 + (-3) becomes the subtraction 5 - 3 of the magnitudes.

Ones' complement. Negation inverts each bit, so -x has the same bits as ~x. "That is, inverting all of the bits of a number (the logical complement) produces the same result as subtracting the value from 0."2 An N-bit form holds -(2ᴺ⁻¹ - 1) to 2ᴺ⁻¹ - 1. Addition uses a binary adder with one extra step. When a carry goes out of the top bit, "the bit must be added back in at the right-most bit"2. This step is the end-around carry.

def add_ones(a, b, bits=8):
    mask = (1 << bits) - 1
    total = a + b
    if total > mask:  # a carry left the top bit
        total = (total & mask) + 1  # the end-around carry
    return total & mask


def value(bits):
    return -(~bits & 0xFF) if bits & 0x80 else bits


four, minus_three = 0b00000100, 0b11111100
s = add_ones(four, minus_three)
print(f"{four:08b} + {minus_three:08b} = {s:08b} = {value(s)}")
s = add_ones(0b00000101, 0b11111010)
print(f"5 + -5 = {s:08b} = {value(s)}")
Output
00000100 + 11111100 = 00000001 = 1
5 + -5 = 11111111 = 0

The second sum gives the negative zero. A program must treat both zeros as equal: "the existence of two forms of the same value necessitates two comparisons when checking for equality with zero."1

Why two's complement won. Two's complement needs no end-around carry and no sign test, and it has one zero. Wikipedia says that "Two's complement is the easiest to implement in hardware, which may be the ultimate reason for its widespread popularity."1 Sign-magnitude machines also had a cost: "These systems did ones' complement math internally, so numbers would have to be converted to ones' complement values when they were transmitted from a register to the math unit"1.

The standards. C and C++ allowed all three forms for a long time. The proposal P0907 says that C11 "allows three representations for signed integral types"3, and that "None of [MSVC], [GCC], and [LLVM] support other representations."3 The old minimum range of an 8-bit signed type was "-127 to 127"4, which "corresponds to the limits of ones' complement or sign-and-magnitude"5. As of C++20, two's complement "is the only representation allowed by the standard"5, and C23 did the same4.

In languages

LanguageVerdictNote
PythonIdiom
C++Idiom
JavaIdiom
CIdiom
C#Idiom
JavaScriptIdiom
TypeScriptIdiomTypeScript uses the bitwise operators of JavaScript, so a program makes the two forms by hand, as in the JavaScript example. No example file exists.
GoIdiom
RustIdiom
KotlinIdiomKotlin's integer types are two's complement, as in Java, so a program makes the two forms by hand with or, inv and and. No example file exists because kotlinc is not installed.
SQLDoes not applySQL integer types have ranges, but SQL does not show the bits of a value.
BashIdiomBash arithmetic has the operators |, ~ and &, so a script can make the two forms by hand. No example file exists.
PowerShellIdiomPowerShell uses the .NET integer types and has the -bor, -bnot and -band operators, so a script makes the two forms by hand. No example file exists because pwsh is not installed.
HTMLDoes not applyHTML has no numbers.
CSSDoes not applyCSS has integer values, such as z-index, but no integer types with a width.
Regular expressionsDoes not applyA pattern matches digits as text but has no integer types.
Language Verdict Where the forms appear
Python idiom bit operations by hand; -0.0 is a sign-magnitude float
JavaScript idiom bit operations by hand; -0 and Object.is
TypeScript idiom as JavaScript
Java idiom bit operations by hand; -0.0 and Double.compare
Kotlin idiom bit operations by hand; -0.0
C# idiom bit operations by hand; -0.0 and double.IsNegative
Go idiom bit operations by hand; math.Copysign and math.Signbit
Rust idiom bit operations by hand; -0.0 and is_sign_negative
C idiom allowed for int before C23; signbit for floats
C++ idiom allowed for int before C++20; std::signbit for floats
Bash idiom bit operations in $(( ))
PowerShell idiom the .NET operators
SQL n/a SQL does not show the bits of a value
Regex n/a no numbers
HTML n/a no numbers
CSS n/a no integer types with a width

No language in use today stores its integers in these forms, so a program that reads such data converts it by hand. Two places still use them. Floating-point numbers store their significand in sign-magnitude: "Sign–magnitude is the most common way of representing the significand in floating-point values."1 So each language with IEEE 754 floats has a -0.0 that is equal to 0.0 but has another sign bit. The Internet checksum uses ones' complement addition.

The example files show the same two facts in each language. They convert 8-bit values to sign-magnitude and to ones' complement with bit operations, and they show the negative zero of a float.

PythonGitHub
import math


def to_sign_magnitude(n):
    return 0x80 | -n if n < 0 else n


def to_ones(n):
    return ~(-n) & 0xFF if n < 0 else n


def from_sign_magnitude(b):
    return -(b & 0x7F) if b & 0x80 else b


def from_ones(b):
    return -(~b & 0xFF) if b & 0x80 else b


print(f"-43 in sign-magnitude: {to_sign_magnitude(-43):08b}")
print(f"-43 in ones' complement: {to_ones(-43):08b}")
print(f"10101011 in sign-magnitude: {from_sign_magnitude(0b10101011)}")
print(f"11010100 in ones' complement: {from_ones(0b11010100)}")
print(f"10000000 in sign-magnitude: {from_sign_magnitude(0b10000000)}")
print(f"11111111 in ones' complement: {from_ones(0b11111111)}")

z = -0.0
print("-0.0 == 0.0:", "yes" if z == 0.0 else "no")
print("-0.0 prints as:", z)
print("math.copysign(1.0, -0.0):", math.copysign(1.0, z))
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
-0.0 == 0.0: yes
-0.0 prints as: -0.0
math.copysign(1.0, -0.0): -1.0
  • Verdict: idiom. Python's int acts as a two's complement value in bitwise operations, so a program makes the other two forms by hand with |, ~ and &.
  • The two negative zeros become the integer 0, because Python int has one zero.
  • A float -0.0 is equal to 0.0, but it keeps its sign bit. math.copysign reads that bit.
#include <bitset>
#include <cmath>
#include <iostream>

unsigned to_sign_magnitude(int n) { return n < 0 ? 0x80u | static_cast<unsigned>(-n) : static_cast<unsigned>(n); }
unsigned to_ones(int n) { return n < 0 ? ~static_cast<unsigned>(-n) & 0xFFu : static_cast<unsigned>(n); }
int from_sign_magnitude(unsigned b) { return (b & 0x80u) ? -static_cast<int>(b & 0x7Fu) : static_cast<int>(b); }
int from_ones(unsigned b) { return (b & 0x80u) ? -static_cast<int>(~b & 0xFFu) : static_cast<int>(b); }

int main() {
    std::cout << "-43 in sign-magnitude: " << std::bitset<8>(to_sign_magnitude(-43)) << '\n';
    std::cout << "-43 in ones' complement: " << std::bitset<8>(to_ones(-43)) << '\n';
    std::cout << "10101011 in sign-magnitude: " << from_sign_magnitude(0b10101011u) << '\n';
    std::cout << "11010100 in ones' complement: " << from_ones(0b11010100u) << '\n';
    std::cout << "10000000 in sign-magnitude: " << from_sign_magnitude(0b10000000u) << '\n';
    std::cout << "11111111 in ones' complement: " << from_ones(0b11111111u) << '\n';

    double z = -0.0;
    std::cout << "-0.0 == 0.0: " << (z == 0.0 ? "yes" : "no") << '\n';
    std::cout << "std::signbit(-0.0): " << (std::signbit(z) ? "yes" : "no") << '\n';
    std::cout << "-0.0 prints as: " << z << '\n';
}
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
-0.0 == 0.0: yes
std::signbit(-0.0): yes
-0.0 prints as: -0
  • Verdict: idiom. C++20 allows only two's complement for signed integers, so a program makes the other two forms by hand with unsigned operations.
  • std::bitset<8> prints the low 8 bits of a value.
  • std::signbit reads the sign of a float, and the stream prints -0.0 as -0.
JavaGitHub
public class Main {
  static int toSignMagnitude(int n) { return n < 0 ? 0x80 | -n : n; }
  static int toOnes(int n) { return n < 0 ? ~-n & 0xFF : n; }
  static int fromSignMagnitude(int b) { return (b & 0x80) != 0 ? -(b & 0x7F) : b; }
  static int fromOnes(int b) { return (b & 0x80) != 0 ? -(~b & 0xFF) : b; }

  static String bin8(int b) {
    return String.format("%8s", Integer.toBinaryString(b)).replace(' ', '0');
  }

  public static void main(String[] args) {
    System.out.println("-43 in sign-magnitude: " + bin8(toSignMagnitude(-43)));
    System.out.println("-43 in ones' complement: " + bin8(toOnes(-43)));
    System.out.println("10101011 in sign-magnitude: " + fromSignMagnitude(0b10101011));
    System.out.println("11010100 in ones' complement: " + fromOnes(0b11010100));
    System.out.println("10000000 in sign-magnitude: " + fromSignMagnitude(0b10000000));
    System.out.println("11111111 in ones' complement: " + fromOnes(0b11111111));

    double z = -0.0;
    System.out.println("-0.0 == 0.0: " + (z == 0.0 ? "yes" : "no"));
    System.out.println("Double.compare(-0.0, 0.0): " + Double.compare(z, 0.0));
    System.out.println("1 / -0.0: " + 1 / z);
  }
}
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
-0.0 == 0.0: yes
Double.compare(-0.0, 0.0): -1
1 / -0.0: -Infinity
  • Verdict: idiom. Java's integral types are two's complement, so a program makes the other two forms by hand with |, ~ and &.
  • == says that -0.0 and 0.0 are equal, but Double.compare puts -0.0 first.
#include <math.h>
#include <stdio.h>

static unsigned to_sign_magnitude(int n) { return n < 0 ? 0x80u | (unsigned)-n : (unsigned)n; }
static unsigned to_ones(int n) { return n < 0 ? ~(unsigned)-n & 0xFFu : (unsigned)n; }
static int from_sign_magnitude(unsigned b) { return (b & 0x80u) ? -(int)(b & 0x7Fu) : (int)b; }
static int from_ones(unsigned b) { return (b & 0x80u) ? -(int)(~b & 0xFFu) : (int)b; }

static const char *bin8(unsigned b, char out[9]) {
    for (int i = 0; i < 8; i++) {
        out[i] = (b >> (7 - i)) & 1u ? '1' : '0';
    }
    out[8] = '\0';
    return out;
}

int main(void) {
    char buf[9] = {0};
    printf("-43 in sign-magnitude: %s\n", bin8(to_sign_magnitude(-43), buf));
    printf("-43 in ones' complement: %s\n", bin8(to_ones(-43), buf));
    printf("10101011 in sign-magnitude: %d\n", from_sign_magnitude(0xABu));
    printf("11010100 in ones' complement: %d\n", from_ones(0xD4u));
    printf("10000000 in sign-magnitude: %d\n", from_sign_magnitude(0x80u));
    printf("11111111 in ones' complement: %d\n", from_ones(0xFFu));

    double z = -0.0;
    printf("-0.0 == 0.0: %s\n", z == 0.0 ? "yes" : "no");
    printf("signbit(-0.0): %s\n", signbit(z) ? "yes" : "no");
    printf("1 / -0.0: %f\n", 1.0 / z);
    return 0;
}
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
-0.0 == 0.0: yes
signbit(-0.0): yes
1 / -0.0: -inf
  • Verdict: idiom. Before C23 an implementation could use either form for its signed types, but C23 allows only two's complement. A program that reads old data makes the forms by hand with unsigned operations.
  • The functions use unsigned so that ~ and & act on known bits.
  • signbit from <math.h> reads the sign of a float, and it finds the sign of -0.0.
using System;

static int ToSignMagnitude(int n) => n < 0 ? 0x80 | -n : n;
static int ToOnes(int n) => n < 0 ? ~-n & 0xFF : n;
static int FromSignMagnitude(int b) => (b & 0x80) != 0 ? -(b & 0x7F) : b;
static int FromOnes(int b) => (b & 0x80) != 0 ? -(~b & 0xFF) : b;
static string Bin8(int b) => Convert.ToString(b, 2).PadLeft(8, '0');

Console.WriteLine($"-43 in sign-magnitude: {Bin8(ToSignMagnitude(-43))}");
Console.WriteLine($"-43 in ones' complement: {Bin8(ToOnes(-43))}");
Console.WriteLine($"10101011 in sign-magnitude: {FromSignMagnitude(0b10101011)}");
Console.WriteLine($"11010100 in ones' complement: {FromOnes(0b11010100)}");
Console.WriteLine($"10000000 in sign-magnitude: {FromSignMagnitude(0b10000000)}");
Console.WriteLine($"11111111 in ones' complement: {FromOnes(0b11111111)}");

double z = -0.0;
Console.WriteLine($"-0.0 == 0.0: {(z == 0.0 ? "yes" : "no")}");
Console.WriteLine($"double.IsNegative(-0.0): {(double.IsNegative(z) ? "yes" : "no")}");
Console.WriteLine($"1 / -0.0 is negative infinity: {(double.IsNegativeInfinity(1 / z) ? "yes" : "no")}");
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
-0.0 == 0.0: yes
double.IsNegative(-0.0): yes
1 / -0.0 is negative infinity: yes
  • Verdict: idiom. The C# integral types are two's complement, so a program makes the other two forms by hand with |, ~ and &.
  • double.IsNegative reads the sign bit, so it finds the sign of -0.0.
JavaScriptGitHub
const toSignMagnitude = (n) => (n < 0 ? 0x80 | -n : n);
const toOnes = (n) => (n < 0 ? ~-n & 0xff : n);
const fromSignMagnitude = (b) => (b & 0x80 ? -(b & 0x7f) : b);
const fromOnes = (b) => (b & 0x80 ? -(~b & 0xff) : b);
const bin8 = (b) => b.toString(2).padStart(8, "0");
const yesNo = (b) => (b ? "yes" : "no");

console.log(`-43 in sign-magnitude: ${bin8(toSignMagnitude(-43))}`);
console.log(`-43 in ones' complement: ${bin8(toOnes(-43))}`);
console.log(`10101011 in sign-magnitude: ${fromSignMagnitude(0b10101011)}`);
console.log(`11010100 in ones' complement: ${fromOnes(0b11010100)}`);
console.log(`10000000 in sign-magnitude: ${fromSignMagnitude(0b10000000)}`);
console.log(`11111111 in ones' complement: ${fromOnes(0b11111111)}`);

const z = fromSignMagnitude(0b10000000);
console.log(`the result is -0: ${yesNo(Object.is(z, -0))}`);
console.log(`-0 === 0: ${yesNo(-0 === 0)}`);
console.log(`Object.is(-0, 0): ${yesNo(Object.is(-0, 0))}`);
console.log(`1 / -0: ${1 / -0}`);
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
the result is -0: yes
-0 === 0: yes
Object.is(-0, 0): no
1 / -0: -Infinity
  • Verdict: idiom. The JavaScript bitwise operators use 32-bit two's complement values, so a program makes the other two forms by hand.
  • A JavaScript number is a float, so -(0) gives -0. A template string prints -0 as 0, but Object.is sees the difference.
  • -0 === 0 is true, so code that must find a negative zero uses Object.is or 1 / x.
package main

import (
	"fmt"
	"math"
)

func toSignMagnitude(n int) int {
	if n < 0 {
		return 0x80 | -n
	}
	return n
}

func toOnes(n int) int {
	if n < 0 {
		return ^(-n) & 0xFF
	}
	return n
}

func fromSignMagnitude(b int) int {
	if b&0x80 != 0 {
		return -(b & 0x7F)
	}
	return b
}

func fromOnes(b int) int {
	if b&0x80 != 0 {
		return -(^b & 0xFF)
	}
	return b
}

func yesNo(b bool) string {
	if b {
		return "yes"
	}
	return "no"
}

func main() {
	fmt.Printf("-43 in sign-magnitude: %08b\n", toSignMagnitude(-43))
	fmt.Printf("-43 in ones' complement: %08b\n", toOnes(-43))
	fmt.Println("10101011 in sign-magnitude:", fromSignMagnitude(0b10101011))
	fmt.Println("11010100 in ones' complement:", fromOnes(0b11010100))
	fmt.Println("10000000 in sign-magnitude:", fromSignMagnitude(0b10000000))
	fmt.Println("11111111 in ones' complement:", fromOnes(0b11111111))

	z := math.Copysign(0, -1)
	fmt.Println("z == 0:", yesNo(z == 0))
	fmt.Println("math.Signbit(z):", yesNo(math.Signbit(z)))
	fmt.Println("1 / z:", 1/z)
	lit := -0.0
	fmt.Println("math.Signbit(-0.0 literal):", yesNo(math.Signbit(lit)))
}
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
z == 0: yes
math.Signbit(z): yes
1 / z: -Inf
math.Signbit(-0.0 literal): no
  • Verdict: idiom. Go's signed integers are two's complement, so a program makes the other two forms by hand. Go writes bitwise NOT as the unary ^.
  • The constant -0.0 is an exact constant, and exact zero has no sign, so lit is positive zero. math.Copysign(0, -1) makes a real negative zero.
RustGitHub
fn to_sign_magnitude(n: i32) -> u32 {
    if n < 0 { 0x80 | (-n) as u32 } else { n as u32 }
}

fn to_ones(n: i32) -> u32 {
    if n < 0 { !((-n) as u32) & 0xFF } else { n as u32 }
}

fn from_sign_magnitude(b: u32) -> i32 {
    if b & 0x80 != 0 { -((b & 0x7F) as i32) } else { b as i32 }
}

fn from_ones(b: u32) -> i32 {
    if b & 0x80 != 0 { -((!b & 0xFF) as i32) } else { b as i32 }
}

fn yes_no(b: bool) -> &'static str {
    if b { "yes" } else { "no" }
}

fn main() {
    println!("-43 in sign-magnitude: {:08b}", to_sign_magnitude(-43));
    println!("-43 in ones' complement: {:08b}", to_ones(-43));
    println!("10101011 in sign-magnitude: {}", from_sign_magnitude(0b10101011));
    println!("11010100 in ones' complement: {}", from_ones(0b11010100));
    println!("10000000 in sign-magnitude: {}", from_sign_magnitude(0b10000000));
    println!("11111111 in ones' complement: {}", from_ones(0b11111111));

    let z = -0.0_f64;
    println!("-0.0 == 0.0: {}", yes_no(z == 0.0));
    println!("(-0.0).is_sign_negative(): {}", yes_no(z.is_sign_negative()));
    println!("1.0 / -0.0: {}", 1.0 / z);
}
Output
-43 in sign-magnitude: 10101011
-43 in ones' complement: 11010100
10101011 in sign-magnitude: -43
11010100 in ones' complement: -43
10000000 in sign-magnitude: 0
11111111 in ones' complement: 0
-0.0 == 0.0: yes
(-0.0).is_sign_negative(): yes
1.0 / -0.0: -inf
  • Verdict: idiom. Rust's signed integers are two's complement, so a program makes the other two forms by hand. Rust writes bitwise NOT as !.
  • is_sign_negative reads the sign bit of a float, so it finds the sign of -0.0.
  • Two's complement. The form that replaced both. See Two's complement.
  • Offset binary. A value is stored as x + K, so the bits sort in the order of the values. The exponent of an IEEE 754 float uses this form. Wikipedia: "The IEEE 754 floating-point standard defines the exponent field of a single-precision (32-bit) number as an 8-bit excess-127 field."1
  • The Internet checksum. IP, UDP and TCP use a ones' complement sum. RFC 1071 says: "Adjacent octets to be checksummed are paired to form 16-bit integers, and the 1's complement sum of these 16-bit integers is formed."6 A receiver checks the sum: "If the result is all 1 bits (-0 in 1's complement arithmetic), the check succeeds."6
  • Negative zero in floats. IEEE 754 floats have +0.0 and -0.0. They compare equal, but 1 / -0.0 is negative infinity. The example files show this. See Floating-point arithmetic.

Pitfalls

  • Two zeros. Code that compares bits instead of values sees two different zeros. In Java, -0.0 == 0.0 is true, but Double.compare puts -0.0 before 0.0, as the Java example shows.
  • A negative zero constant that is not negative. In Go, the constant -0.0 is positive zero, because Go has "no constants denoting the IEEE 754 negative zero, infinity, and not-a-number values"7. The Go example uses math.Copysign(0, -1) instead.
  • Reading old data. Data from a machine with another signed form has the wrong value if it is read as two's complement. In 8 bits, the ones' complement 11111110 is -1, but in two's complement it is -2.
  • Thinking that C still allows them. Code that tests for ones' complement or sign-magnitude, such as a check of -1 & 3, is dead code under C23 and C++20.

Cost

Sign-magnitude needs a sign test and a subtractor for addition, and it converts values for the adder on some machines1. Ones' complement needs the end-around carry, and Wikipedia says: "This phenomenon does not occur in two's complement arithmetic."2 Both forms need two comparisons to test for zero1. Two's complement has none of these costs.

History

"One of the great debates was the format of negative numbers"1 in the early days of computing. "IBM was one of the early supporters of sign–magnitude, with their 704, 709 and 709x series computers being perhaps the best-known systems to use it."1 Ones' complement machines included the UNIVAC 1101, the CDC 160, the CDC 1604, the CDC 6600, the LINC and the DEC PDP-12. "Successors of the CDC 6600 continued to use ones' complement arithmetic until the late 1980s"2. The descendants of the UNIVAC 1107 "continue to use ones' complement"2, but current Unisys ClearPath Dorado machines emulate that instruction set on two's complement processors2.

John von Neumann suggested two's complement in 19458. The IBM System/360 of 1964 "made two's complement the most widely used binary representation in the computer industry."8 The Internet checksum of RFC 1071 (1988) still uses ones' complement sums6. P0907 of 2018 removed the other forms from C++2035, and C23 followed4.

References

  1. Signed number representations, Wikipedia, the introduction; History; Sign–magnitude; Ones' complement
  2. Ones' complement, Wikipedia, the introduction; Number representation; Basics; Negative zero
  3. P0907R4: Signed Integers are Two's Complement, JF Bastien, 2018, Abstract; 1. Introduction; 6. Survey of Signed Integer Representations
  4. Arithmetic types, C reference, cppreference.com, Integer types, Properties
  5. Fundamental types, C++ reference, cppreference.com, Properties
  6. RFC 1071: Computing the Internet Checksum, Braden, Borman and Partridge, 1988, 1. Introduction
  7. The Go Programming Language Specification, Constants
  8. Two's complement, Wikipedia, History

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/9SF2JW/text.yaml