Skip to main content
Mathematics LibreTexts

3.1: Integers

  • Page ID
    53659
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Integers

    Recall that mathematically, an integer is a counting number (1,2,3, ...) along with 0 and the negative counting numbers (−1, −2, −3, . . .). Mathematically thinking, there is no largest (or smallest) integer, however, in reality if we are storing a number on a computer (which is a finite device), there must be a limit on the smallest and largest integers to be stored. Practically speaking, we will limit an integer to some number of bits and the standard sizes are 8, 16, 32, 64 and 128.

    Unsigned Integers

    First, we will examine unsigned integers and typically these are thought of as the non- negative integers (0,1,2,3, ...). For example, 8-bit unsigned integers have a total of \(2^8 = 256\) nonnegative numbers and since the smallest is 0, the largest is 255.

    In julia, the data types for unsigned integers are UInt8, UInt16, UIint32, UInt64 and UInt128. We can get the smallest and largest value for these with the typemin and typemax functions. 

    typemin(UInt8)
    0x00
    typemax(UInt8)
    0xff

     

    Appendix XXXX covers many of the details of representation integers in binary and performing basic operations. We will cover the a superficial level of integer represenation and operations here in this chapter, but for those with desire for more depth see Appendix XXX

    In julia, we can use the bitstring function to give the binary representation of integers and floating points. For example

    bitstring(UInt8(18))
    "00010010"

    and 

    bitstring(UInt8(255)) 
    "11111111"

    Similarly, the unsigned integers with more bits work the same with largest range of integers. For example 

    bitstring(UInt64(100000))
    "0000000000000000000000000000000000000000000000011000011010100000"

    which is a string of length 64.

    Signed Integers

    In julia, the signed integers are Int8, Int16, Iint32, Int64 and Int128. Also, there is a integer type Int which defaults to the sized integer of the typical integer size on your machine. This is generally Int64.

    Let’s look in detail about 8-bit signed integers. The largest and smallest values that can be stored with Int8 can be found with 

    typemin(Int8)
    -128
    typemax(Int8)
    127

    Bascially the number between 0 and 127 are identical between Int8 and UInt8.

    Overflow and Underflow of integer operations

    Again, unlike mathematical integers, any computer-based integer has a maximum and minimum values. In short, if an operation results in a number above the maximum, then there is an overflow error and less than the minimum there is an underflow error.

    Here’s a simple example with 8-bit integers. Let 

    x=Int8(95)
    95

    and

    y=Int8(70)
    70

    The sum of 95 and 70 is 165 and above the maximum value for Int8. However, entering

    x+y
    -91

    returns a strange result.  Perhaps we expected an error. What just happened? If you want to know why the value of -91 arose, dig into the details in Chapter XXX, but the reason why there was no overflow error is that julia does not automatically check for such errors, due to the fact that there is overhead in checking, which will slow down operations.

    If you want to check, there are a suite of operations that will check, therefore:

    Base.checked_add(x,y)
    OverflowError: 95 + 70 overflowed for type Int8
    
    Stacktrace:
     [1] throw_overflowerr_binaryop(::Symbol, ::Int8, ::Int8) at ./checked.jl:154
     [2] checked_add(::Int8, ::Int8) at ./checked.jl:166
     [3] top-level scope at In[8]:1
     [4] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

    Go to julia’s documentation on checked_add which starts a list of functions that will check for over and underflow. If there is any chance of overflow/underflow errors, then the results may be wrong. Keep this in mind as in Chapter XXXX we will write tests for code.


    This page titled 3.1: Integers is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Peter Staab.

    • Was this article helpful?