Common data type ranges

From Maths
Jump to: navigation, search

TODO: This is just some notes for me to reference at the moment, please only use these IF YOU KNOW THEY APPLY - there are various C conventions...


Signed integers - 2's complement

  • In general, given [ilmath]n[/ilmath] bits, the range of [ilmath]x[/ilmath] (an [ilmath]n[/ilmath]-bit signed two's complement integer) is:
    • [ilmath]-2^{n-1} \le x \le 2^{n-1}-1[/ilmath], which we may write as a closed interval of the integers, [ilmath]x\in[-2^{n-1},\ 2^{n-1}-1][/ilmath]
Bits Range (inclusive) Comment
Signed Unsigned
Low High Low High
8 -128 127 0 255
16 -32,768 32,767 0 65,365 Often associated with older spreadsheet software, only supporting rows from 1 to 65,536. Note the offset[Note 1].
24 -8,388,608 8,388,607 0 16,777,215 Relevant for float - per standard.
32 -2,147,483,648 2,147,483,647 0 4,294,967,295 Notice that 2,147,483,647 is about 68.05 years[Note 2], and 1970+68=2038 - see 2038 problem
64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 0 18,446,744,073,709,551,615 9.2 billion trillion, 18.4 billion trillion using 10^3 increments.

TODO: Notice the lowest digit of each column is the same. Why?


Notes

  1. Internally for rows they'd have used shorts for the job, adding one to the displayed row number as spreadsheets have no row 0.
  2. Calculation:
    • [math]A:\eq\frac{2^{31} -1}{60*60*24*365.25}\approx 68.04965039[/math] which is 68 years and [ilmath](A-68)*365.25\approx 18.135[/ilmath], so:
    Approximately 68 years and 18 days after the first instant of 1970 (UTC) is the maximum time a signed 4 byte integer can represent