Bits and Data

CS-A1120 Programming 2

Lukas Ahrenberg

Department of Computer Science
Aalto University

Module I - the mystery of the computer

  • Aim: to study the principle of a computer - bottom up
    • How to represent data
    • How to build logic circuits that operate on data
    • How to use clocked feedback in circuits
    • How to program a computer without a high-level language (assembly)

We will build this in Scala by using classes that represents logical components. In the end, working on a simulated processor.

A mystery revisited: last week's multiplication

scala> def test(m: Int) = (m * (m + 1))/2
def test(m: Int): Int

scala> test(3)
val res: Int = 6

scala> test(50000000) // 5*10^7
val res: Int = -814376896

scala> def test(m: Long) = (m * (m + 1))/2
def test(m: Long): Long

scala> test(50000000) // 5*10^7
val res: Long = 1250000025000000

scala> test(50000000000)  // 5*10^10
val res: Long = -4378596987249509888

Observations:

  • Int: error for a smaller input
  • Long allows for larger numbers
  • But both seems to be finite
  • As you may know:
    • Int uses 32 bits
    • Long uses 64 bits
  • But why negative numbers?

After this lecture

  • You will have familiarity with binary and hexadecimal numbers
  • You will think of data as sequences of bits, and know some basic data types
  • You know the syntax of Boolean operations and shift operators on integer data types in Scala
  • You will be aware of the IEEE 754 floating point number standard

See this week's reading material for even more information!

Analog and Digital

  • Analog : using continuous phenomena
  • Digital : using discrete set of states

While there has been analog computers, both historical and modern, commercial computers today are digital.

google-data-center.jpg

Google data center
Source: Google gallery

Antikythera-mechanism.jpg

Source: Wikipedia CC 2.5 Generic

Digits

  • Digit = finger/toe
  • The decimal number system has ten digits: 0,1,2,3,4,5,6,7,8,9
  • The binary system has two: 0,1

Bits

  • Bit (short for "binary digit")
  • Basic unit for representing information

zero-and-one.png

Claude E. Shannon A mathematical theory of communication (1948) reports that J. W. Tukey introduced "bit" to represent a number 0 or 1 in a binary numeral system.

(Usually denoted as 0 and 1, but the symbols are not important.)

Boolean operations on bits

boolean-ops.png

XOR stands for eXclusive OR

  • AND : "minimum"
  • OR : "maximum"
  • XOR : "sum, parity"
  • NOT : "toggle"

Forcing and toggling the value of a bit

boolean-force-toggle.png

Data

bit-sequence.png

  • Information in computing is represented as sequences of bits ("data")
  • The basic measure of amount of data is the number of bits in the sequence

Word, word length

  • Most computers are build to process data in units of fixed number of bits (e.g. 8, 16, 32, 64, or 128 bits)
    • (We will see why in later rounds)
  • Units like this are called words
  • In a computer architecture the word length can be thought of as determining operation capacity
  • When people talk about a 64 bit processor this typically means that the word length is 64 bits
  • Larger data is broken up into sequences (or other structures) of words in computer memory

Bit indices in a Word

Example: 16 bit word

word-bit-indices.png

(Same as for decimal numbers, the least significant digit is the rightmost one.)

With \(n\) bits, we can represent different \(2^{n}\) states

  • One bit allows us to represent (tell apart) two different states (things)
  • Say, we let 0 = πŸ’ and 1 = 🍍 we can communicate the difference between cherries and pineapples
  • With 2 bits we can differentiate between 4 states

bits Interpretation
00 = 0
01 = 1
10 = 2
11 = 3

bits Interpretation
00 = a
01 = b
10 = c
11 = d

bits Interpretation
00 = πŸ’
01 = 🍍
10 = 🍏
11 = πŸ‰

  • With \(n\) bits there are \(2^{n}\) bit strings of length \(n\)
    • Int is 32 bit, so \(2^{32}\) different states
    • Long is 64 bit, so \(2^{64}\) different states

Representing natural numbers (non-negative integers)

  • The number "754" can be represented in various ways:
    • Roman numerals: DCCLIV
    • Decimal numerals: 754
    • Binary numerals: 1011110010

The Decimal and Binary representations are both examples of positional number systems with different base (or radix).

If there is a risk of confusion, the base is represented as a suffix to the number, e.g. \((11)_{2}\) or \((11)_{10}\)

Positional number systems

  • In a positional number system we have a set of symbols, the size of which we call the base or radix
    • In our usual decimal number system this is 0,1,2,3,4,5,6,7,8,9, meaning we have base 10
    • In the binary number system we use 0,1, and the base is 2
    • In programming we also use the hexadecimal number systems, usually with the symbols 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, and thus base 16

Note: The symbols are not important For base \(n\) we only need a order of \(n\) different symbols (states). What they are isn't that important (decimal in 0-9 or why not A-J, binary in 0 or 1, or green and blue, or …)

Interpreting positional numbers

  • We write out the number as a string, where the least significant digit is the rightmost, and the most significant digit is the leftmost,
  • and calculate the value of a position as \(b^i\) where \(b\) is the base, and \(i\) is an index starting from \(0\) at the least significant digit
    • For example: \(754 = 700 + 50 + 4 = 7 \times 10^{2} + 5 \times 10^{1} + 4 \times 10^{0}\)
    • The same is possible when interpreting binary data as a decimal number:
\begin{gather*} \left(1011110010\right)_{2} = 1 \times 2^{9} + 0 \times 2^{8} + 1 \times 2^{7} + 1 \times 2^{6}\\ + 1 \times 2^{5} + 1 \times 2^{4} + 0 \times 2^{3} + 0 \times 2^{2} + 1 \times 2^{1} + 0 \times 2^{0}\\ = 512 + 128 + 64 + 32 + 16 + 2\\ = 754\\ \end{gather*}

From binary to decimal (non-negative numbers)

  • As we have seen, we can use the same 'formula' as for decimal numbers, but with base 2.
  • Or, expressed as an algorithm:
    • Input, base 2 number \(x \in \mathcal{Z}_{\geq 0}\)
    • Initialize result to value \(0\)
    • Repeat for every bit position \(j \geq 0\)
      • if bit \(j \) is 1 then
        • Add \(2^{j}\) to result
  • Example: \(\left(11001\right)_2 = 2^0 + 2^3 + 2^4 = 25\)

From decimal to binary (non-negative numbers)

  • Input, base 10 number \(x \in \mathcal{Z}_{\geq 0}\)
  • Initialize all bits in the result to 0 (off)
  • Repeat until \(x = 0\)
    • Find the largest possible \(y = 2^j\) such that \(x - 2^j \geq 0\)
    • Set bit \(j\) to 1 (on)
    • Update \(x\) to \(x - y\)

Example: Convert 25 to binary

  1. Initially \(x = 25\), \(res = 0\)
  2. Find largest \(2^j \leq 25\)
    • This is \(y = 16\), so \(j = 4\)
    • Set bit 4 to 1: \(res = \left(10000\right)_2 \)
    • Update: \(x = 25 - 16 = 9\)
  3. Find largest \(2^j \leq 9\)
    • This is \(y = 8\), so \(j = 3\)
    • Set bit 3 to 1: \(res = \left(11000\right)_2\)
    • Update: \(x = 9 - 8 = 1\)
  4. Find largest \(2^j \leq 1\)
    • This is \(y = 1\), so \(j = 0\)
    • Set bit 0 to 1: \(res = \left(11001\right)_2\)
    • Update: \(x = 1 - 1 = 0\)
  5. \(x = 0\) so we end

The final result is \(\left(11001\right)_2\)

Binary numbers

  • Computing is easy in the binary numeral system
    • Only two states for the machine to tell apart
    • Multiplication is easy (no tables to memoize)
      • (More on this later)
  • Binary and Hexadecimal numbers are part of general knowledge for a programmer

Nice to know: signed (negative & positive) numbers

  • We need an encoding to represent signed numbers
  • Several available, for example
    • Sign + magnitude (usually, most significant bit is sign), e.g
      • \(\;2_{10} = 0010_2\)
      • \(\text{-}2_{10} = 1010_2\)
    • One's complement (negative number is bit-wise not of positive), e.g.
      • \(\;2_{10} = 0010_2\)
      • \(\text{-}2_{10} = 1101_2\)
    • Two's complement (negative number is bit-wise not of positive + 1), e.g.
      • \(\;2_{10} = 0010_2\)
      • \(\text{-}2_{10} = 1110_2\)
  • Two's complement is used in Scala and many other languages.
    • Has the nice property that adding positive and negative numbers is relatively simple

Two's complement 3 bit numbers

Bits Signed two's comp. Unsigned
000 0 0
001 1 1
010 2 2
011 3 3
100 -4 4
101 -3 5
110 -2 6
111 -1 7

  • Half of the available states represent negative numbers
  • β‡’ the maximum magnitude of the values that can be represented is halved

Lengths of basic integer types in Scala

  • Scala is a hardware-independent language (It is designed to be used with many types of processors.)
  • The basic value types in Scala have the following fixed lengths in bits:
    • Byte : 8 bits
    • Short : 16 bits
    • Int : 32 bits
    • Long : 64 bits

As there are a limited number of bits for each type, adding or multiplication could lead to overflow, where the resulting number is to large to be represented. This is good to be aware of as overflow usually does not raise an error, but the result can be unexpected.

Numerical and Textual data types

  • Most programs involve at least numerical (representing numbers) and textual (representing text) data.
  • The basic data types in Scala can be divided into these two categories

    • numerical

          Byte, Short, Int, Long, Float, Double
      

      Float (32 bit) and Double (64 bit) are for representing real numbers

    • textual

         Char, String
      

      Represents single characters and strings (String is actually a sequence of Char)

What does data represent ?

  • A sequence of bits is just a string of zeroes and ones
  • What makes the sequence of bits interesting is the conventions and standards on what the sequence represents

bit-sequence.png

\(29058\) bits

Take a particular sequence of 29058 bits (which doesn't fit in one line or on the page):

111111111111111111111110001111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000111001111111111111111111111111111101111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111000110011110111100111011111111111111011111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111100100111011111000110011111100111110111111011011111011101111100011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111101011100111101100110111111011111101111100110111110011001110000111000011111111111001111111111111111111111111111111111111111111111111111111111111111111111111111001111111010110011111011101100111101111111011111001001111110110001101101110110011100111110011111111111111111111111111111111111111111111111111111111111111111111111111110111111110100011111110111011101110011111110111110010011111101101010110011101110111000111101111111111111111111111111111111111111111111111111111111111111111111111111111101111111100000111111101110011001101111111101111101100111111011010001100111111101110110111101111111111111111111111111111111111111111111111111111111111111111111111111111011111111000000011111000000111000111111111011111011001111111110110111001101111011101101111011111111111111111111111111111111111111111111111111111111111111111111111111110111111110001110011100001101110001111111110000000110011111011101111110011011110111011101110111111111111111111111111111111111111111111111111111111111111111111111111111100111111001011110011011111011110111111111101111101101111110111011111100110111101111011011101111111111111111111111111111111111111111111111111111111111111111111111111111001111110110111110110111110011101111111111011111011011111101100111111011100000001110110011011111111111111111111111111111111111111111111111111111111111111111111111111111001111001101111100101111110111011111111110111110110011110011001111110111001111011101110110111111111111111111111111111111111111111111111111111111111111111111111111111111001100111011111100011111101110111011111101111101110111101110011111101110111110111011100100111111111111111111111111111111111111111111111111111111111111111111111111111111000011110111111110111111011101110111111011111011100100111100111111001001111101110111101001111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111001111111111111111100011111111111111010011111001101111000111001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111011011111001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111111111011111111111111111111111110111111111111111111111111110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110011111101110111111111111111111111000000011111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100111111011101111110110011111111100111110111111111111111111110011101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111110110011111101110111111011011111110111111111111111111100011011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111101100111111001101111110111111111001111111111111111111100000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111011011111110011011111101111111110111111111111111111111001001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111100110111111110110111111011111111011111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000001101111111011101111110111111000111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110001110011011111110111000000001111100111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111110110111111101110000000011111001111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111101110111111011101111110111110011111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111011101111110111011111101111110111111111111111111111001110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111110111011111011110111111011111111111111111111111111110001101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111100110011000111101111110111111111111111111111111111101101011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111000111111011111101111111111111111111111111111011100111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111000111111111111111111110111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111110011111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110011111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111110110000110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111000001100000000001100000000001111111111111111111001111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111010111111110011111101100011011111111000000111111101111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111110100111111110111110111110110000001110111111111111101111101100111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111011101111111101111101111110101111101101111111111111001111010000011101111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011100111011111111011111011111110011111011011111111111111001101001110011011111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111110100000110000111110111101111111100111100111011111111111111010010111110010111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111110101111111101100011111111001110111110001111111111111001101111110101111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111101011111111000010111111110100011111100000111111111110011011111101011111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111010111111110111101111111111000111111011111111111111101110111111010111111101111111111111111111100000000111111111111111111111111111111111111111111111111111111111101111101101111111101111101111110110110011110111111111111111101101111110101111110111111111111111111100111111110001111111111111111111111111111111111111111111111111111111011111011011111111011111011111101101110111101111111111111111010111111011011111101111111111111111110011111111111100111111111111111111111111111111111111111111111111111110111101110000000110111111011110111011110111011111111111111110100111110110111111011111111111111111011111111111111110111111111111111111111111111111111111111111111111111100000111111000111101111111011001110111101110000000111111111101101111011110111110111111111111111100111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111000111111111011100011111111111110011001101111100111011111111111111111011111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100111111110001111111111111111100111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111011111111111111111111111111111111111111111111110111111111111111111111111101111011111111111111111111111111111111111111111111111110111111111111101111111111111111111111110111111111111111111111111111111111111111111111001111111111111111111111111111100111111111111111111111111111111111111111111111111110111111111111011111111111111111111111101111111111111111111111011111111111111111111111011110111110000000000111111110110110011111111111001111000011111111111111111111111100111111111110111111111111111111111111011111111111111111111100001111111111111111111110111101110111100111111111111011101000000110011100011001111011011101111110111110011101111111111001111111111111111111111110111111111111111111111011011111111111111111111001111011101111011111111111100111110111101100111010010111110010111011111100000001111111111111110011111111111111111111111101111111111111111111110110111111111111111111110111110111011110111111111111011111011111101000101110101111110101110111111001111111111111111111101111111111111111111111111011111111111111111111101101111111111111111111101111101110111101111111111101111110111111010100011101011111001011101111110011111111111111111111001111111111111111111111111111111111111111111111011011111111111111111111000000011101111011111111111011111111111110101101111010111110110111011111110111111111111111111110011111111111111111111111011111111111111111111110110111111111111111111110000000111011110111111111110111110111111101011111101101111011101110111111100000111111111111111100111111111111111111111110111111111111111111111101100011111111111111111101111101110111101111111111011111101111111010111111011010000111011101111111000111111111111111111101111111111111111111111101111111111111111111111011110111111111111111111011111011011111011111111110011111011111110101111110110110111110111011111110111111111111111111111011111111111111111111110111111111111111111111100101101111111111111111110011110110111110111111111110111110111111011011111101101111111101110111111101111111101111111111110111111111111111111111101111111111111111111111011010111111111111111111101111101100111101111111111101111110111110110111111011011111111011101111111011111111001111111111100111111111111111111111011111111111111111111110110101111111111111111111011111011001111011111111111011111100110011101111110110111111110111000000100111111110011111111111101111111111111111111101111111111111111111111101101011111111111111111111111111111111110111111111111011001110011111111111111101111111101110011100100000001100111111111111011111111111111111111011111111111111111111110111010111111111101111111111111111111111101111111111110000111111111111111111111111111111111111111111000011111011111111111111011111111111111111101111111111111111111111101110011111111110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111110011111111111111110011111111111111111111111011010111111111100111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111110011111111111111001111111111111111111111110110101111111110001110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111100111111111111111111111111011101011111111010011001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110001111111110011111111111111111111111110111011111111110100100111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101001111110011111111111111111111111111101110011111110011011011111110011111101110000001111111111111111111111111111111111111111111111111111111111111111111111111111111110011100000011111111111111111111111111111011010111111101110001111111100111111011101111110000011111000000011111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111110110101111111111110111111111011111110110011111100100000000001110110011111110111111111111111111111111111111111111111111000011111001111111111111111111111111111111111111001101011111111111111111111110111111101100111111111101111101111111110111111101111111110111110111111111111111111111111110111011110011111111111111111111111111111111111110111010111111111111111111111101111111011001111111111011111011111111100111111011111111101111101100000000111111111111111001110011100011111111111111111111111111111111111101110101111111111111111111111011111110111000000111110111110111111111001111110111111110011111001001111101111111111111110111110111000011111111111111111111111111111111111011011011111111111111111111110111111101111111101111101111101111111111001111101111111100111111010111111101111111111111101111110110100111111111111111111111111111111111110110101111111111111111111111001111111011111111011111011111011011111101011110111111111001111110101111111011111111111111011111101101000111111111111111111111111111111111101101011111111111111111111110111111110111111110011110111110000111111011011101111111110011111101011111100111111111111110111111001011010111111111111111111111111111111111011010111111111111111111111101111111101111111100111101111101111111100110111011111111110111111010111111011111111111101101111111010110110111111111111111111111111111111110110101111111111111111111111001100111011011111011111011110111111111001110110111111111101111110100000000111111111111011011111110001110101111111111111111111111111111111101001011111111111111111111110000011110111011101111110111100111111110111101101111111111011111011000000111111111111110110111111100011101101111111111111111111111111111111010110111111111111111111111111111111111111000111111101111000000000000111101011111111110111110110111111111111111111100001111111000111001101111111111111111111111111111110101101111111111111111111111111111111111111111111111011111000001111011111000111111111100111011101111111111111111110001011111110001111011001111111111111111111111111111101010011111111111111111111111111111111111111111111111111110111111111111111011111111111100110111011111110011111111100110111111110011110111001111111111111111111111111110110000111111111111111111111111111111111111111111111111111111111111111111111111111111111100011110111111001111111111001101111111100111100111001111111111111111111111111101100001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111110101011111111001111101111001111111111111111111111111010001011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101010111111110011111011111001111000000000001111111110100011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111010100111111100111111011111000000111111111000011111011010110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100101101111111001111110111111001111111111111111111110000101101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000011111110011111100111111111111111111111111111100001011011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110010111111000111111100011111111111111111111111111111110111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101100111110101111111110001111111111111111111111111011000010011111111111111111100000000000000111111111111111111111111111111111111111111111111111111111111111111111111111011101111101011111111111000111111111111111111111000000010010000000011010000001111111111111000111111111111111111111111111111111111111111111111111111111111111111111111110111011110010011111111111100011111111111110000000000000000010000000000000111111110001000000001111111111111111111111111111111111111111111111111111111111111111111111111111110011101100111111111111110001111111111100111111011111111101111111100000000000111001111110101111111111111111111111111111111111111111111111111111111111111111111111111111110111011001111111111111111000111111111001111111111111111111111111111111111111111111111001011111111111111111111111111111111111111111111111111111111111111111111111111011110101110011111111111111111100011111110000000000000000000000000000000000000000000000000010011111111111111111111111111111111111111111111111111111111111111111111111110111110011100111111111111111111111001111111111001111111111111111111111111111111111111111100110111111111111111111111111111111111111111111111111111111111111111111111111101111111111001111111111111111111111100001111110011111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111111111111011111111111011111111111111111110111110000111110111111111111111111111111111111111111111110111001111111111111111111111111111111111111111111111111111111111111111111111110111111111110111111111111110000000111111110011001111111111111111111111111111111111111111101110011111111111111111111111111111111111111111111111111111111111111111111111101111111111100111111100000000111100111111000000011111111111111111111111111111111111111111011100111111111111111111111111111111111111111111111111111111111111111111111111011111111111100000000011111111111101111110111101111111111111111111111111111111111111111110111011111111111111111111111111111111111111111111111111111111111111111111111111011111111111000000000000000000000000000101100011111111111111111111111111111111111111111101110111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111001100000110111111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111000000000011111011011111101111111111111111111111111111111111111111110111011111111111111111111111111111111111111111111111111111111111111111111111111011111111110000000000011111111001110010011111011111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111111111111111111111111111110111111100000111111111111111111001110110111110111111111111111111111111111111111111111111011011111111111111111111111111111111111111111111111111111111111111111111111111101111110011111111111111111111111001100011111101111111111111111111111111111111111111111110110111111111111111111111111111111111111111111111111111111111111111111111111111011110011111111111111111111111111011100111111011111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111111111111111111111111111110011101111111111111111111111111110011101111110111111111111111111111111111111111111111111011011111111111111111111111111111111111111111111111111111111111111111111111111110111011111111111111111111111111110111001111101111111111111111111111111111111111111111110110111111111111111111111111111111111111111111111111111111111111111111111111111101110111111111111111111111111111001110011111011111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111111111111111111111111111111011110111111111111111111111111110111100111110111111111111111111111111111111111111111111011011111111111111111111111111111111111111111111111111111111111111111111111111110111110011111111111111111111110011111000111101111111111111111111111111111111111111111110110111111111111111111111111111111111111111111111111111111111111111111111111111101111110000001111111111111110001111101101111011111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111111111111111101111111111100000000000000001111111011011110111111111111111111111111111111111111111110111011111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111101101111111111111100110011101111111111111111111111111111111111111111101110011111111111111111111111111111111111111111111111111111111111111111111111111110000000000011110011011111111111111011110111011111111111111111111111111111111111111111101100111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000011111111111100111101110111111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000111111111111011111101101111111111111111111111111111111111111111110111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111011111111111110111111011011111111111111111111111111111111111111111101110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001110111111111111001111110010111111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111101111111111110111111110101111111111111111111111111111111111111111111011011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111011111111111001111111101011111111111111111111111111111111111111111110110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111110011110111111111110111111111010111111111111111111111111111111111111111111101110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111101111111111101111111111100111111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111111111111111111111111110011111001111111110011111111111001111111111111111111111111111111111111111110111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111011111111101111111111110111111111111111111111111111111111111111111101110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111110111111110011111111111101111111111111111111111111111111111111111111001101111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111101111111101111111111111011111111111111111111111111111111111111111110011001111111111111111111111111111111111111111111111111111111111111111111111111111111111110011111111001111111111111111111110111111111111111111111111111111111111111111100111011111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111011111111111111111111101111111111111111111111111111111111111111111101110011111111111111111111111111111111111111111111111111111111111111111111111111111111110010010111110011111111111111111111011111111111111111111111111111111111111111111011110111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000011111111111111111110111111111111111111111111111111111111111111110111100111111111111111111111111111111111111111111111111111111111111111111111111111111100111111111111110011111111111111111101111111111111111111111111111111111111111111100000000011111111111111111111111111111111111111111111111111111111111111111111111111110011000000000001110011111111111111111011111111111111111111111111111111111111111111001110000000011111111111111111100000000101111111111111111111111111111111111111111111000000011111111100000011111111111111110111111111111111111111111111111111111111111110000000111111000000000000110001111111111100000000111111111111111111111111111111111100101111111111111111100011111111111111101111111100011111111111111111111111111111111001110111111111111111111111111111111111111111111111111111111111111111111111111111111011101111111111111110000111111111111111000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111010011111111111111101101111111111111110001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110001111111111111111011011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

This could be intepreted as a very large integer number, or..

\(29058 = 167 \times 174\) bits

Divided in to 174 rows, each of 167 columns:

11111111111111111111111000111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111110000001110011111111111111111111111111111011111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111110111000110011110111100111011111111111111011111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111100111110010011101111100011001111110011111011111101101111101110111110001111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111101111111010111001111011001101111110111111011111001101111100110011100001110000111111111110011111111111111111111111111111111111111111111111111111111 11111111111111111111001111111010110011111011101100111101111111011111001001111110110001101101110110011100111110011111111111111111111111111111111111111111111111111111111 11111111111111111111011111111010001111111011101110111001111111011111001001111110110101011001110111011100011110111111111111111111111111111111111111111111111111111111111 11111111111111111111011111111000001111111011100110011011111111011111011001111110110100011001111111011101101111011111111111111111111111111111111111111111111111111111111 11111111111111111111011111111000000011111000000111000111111111011111011001111111110110111001101111011101101111011111111111111111111111111111111111111111111111111111111 11111111111111111111011111111000111001110000110111000111111111000000011001111101110111111001101111011101110111011111111111111111111111111111111111111111111111111111111 11111111111111111111001111110010111100110111110111101111111111011111011011111101110111111001101111011110110111011111111111111111111111111111111111111111111111111111111 11111111111111111111001111110110111110110111110011101111111111011111011011111101100111111011100000001110110011011111111111111111111111111111111111111111111111111111111 11111111111111111111100111100110111110010111111011101111111111011111011001111001100111111011100111101110111011011111111111111111111111111111111111111111111111111111111 11111111111111111111110011001110111111000111111011101110111111011111011101111011100111111011101111101110111001001111111111111111111111111111111111111111111111111111111 11111111111111111111111000011110111111110111111011101110111111011111011100100111100111111001001111101110111101001111111111111111111111111111111111111111111111111111111 11111111111111111111111101111111111111111111111111111100111111111111111110001111111111111101001111100110111100011100111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111110110111110011111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111100111111111111111111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111100111111111101111111111111111111111111011111111111111111111111111001111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111100111111011101111111111111111111110000000111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111100111111011101111110110011111111100111110111111111111111111110011101111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111100111111011001111110111011111101101111111011111111111111111110001101111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111011001111110011011111101111111110011111111111111111111000000111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111011011111110011011111101111111110111111111111111111111001001111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111110011011111111011011111101111111101111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110000000011011111110111011111101111110001111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110001110011011111110111000000001111100111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111011011111110111000000001111100111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111011101111110111011111101111100111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111011101111110111011111101111110111111111111111111111001110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111011101111101111011111101111111111111111111111111111000110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110011111001100110001111011111101111111111111111111111111111011010111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111110111111111111000111111011111101111111111111111111111111111011100111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111101111100011111111111111111111011110111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111101111100111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111001111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111110111111111111111111111111111111111111111111101100001100111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111100111111111111111111111111111111111111111111000001100000000001100000000001111111111111111111001111110111111111111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101111101011111111001111110110001101111111100000011111110111111011111111111111111111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101111101001111111101111101111101100000011101111111111111011111011001111111111110111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101111011101111111101111101111110101111101101111111111111001111010000011101111110111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101110011101111111101111101111111001111101101111111111111100110100111001101111110111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101000001100001111101111011111111001111001110111111111111110100101111100101111111011 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101111110101111111101100011111111001110111110001111111111111001101111110101111111011 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101111110101111111100001011111111010001111110000011111111111001101111110101111111011 11111111111111111111111111111111111111111111111111111111111111111111111111111111111101111110101111111101111011111111110001111110111111111111111011101111110101111111011 11111111111111111100000000111111111111111111111111111111111111111111111111111111111101111101101111111101111101111110110110011110111111111111111101101111110101111110111 11111111111111110011111111000111111111111111111111111111111111111111111111111111111101111101101111111101111101111110110111011110111111111111111101011111101101111110111 11111111111111100111111111111001111111111111111111111111111111111111111111111111111101111011100000001101111110111101110111101110111111111111111101001111101101111110111 11111111111111011111111111111110111111111111111111111111111111111111111111111111111100000111111000111101111111011001110111101110000000111111111101101111011110111110111 11111111111110011111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111100011111111101110001111111111111001100110111110011101111 11111111111110111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111111100011111 11111111111100111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111101111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

continues…
… and interpreted such that 0 is black and 1 is white, will result in

okay-human-xkcd-371.png

Detail from: https://xkcd.com/371/

Data format

Interpretation is all about the standard / format / convention

  • A sequence of bits (data) can be interpreted in many ways (although not all may make sense)
  • We need a convention of what the data represents
  • These are called formats, or
  • (in programming languages) referred to as the data types

For example, the bits

\(0100000000001001001000011111101101010100010001000010110100011000\),

interpreted as a Scala Double floating point value is \(3.141592653589793115997963468544185161590576171875\)

while interpreted as a Scala Long integer value it is \(4614256656552045848\).

64 bits as a floating point number

0100000000001001001000011111101101010100010001000010110100011000

interpreted according to the IEEE Std 754 floating point standard is

\(3.141592653589793115997963468544185161590576171875\)

  • This is the number you will get in Scala by using the build-in constant scala.math.Pi
    • Note that this is not the exact mathematical constant \(\pi\) (Why?)
  • Float = IEEE 754 'binary32'
  • Double = IEEE 754 'binary64'

Floating point numbers

scala> 0.3 - 0.1

Result?

val res10: Double = 0.19999999999999998

What is going on here?

Floating point standard

  • IEEE Std 754
  • Finite number of bits β†’ Not all rational numbers can be represented
    • Moreover, which numbers that can be represented with a finite number of digits depends on the numerical base
  • IEEE 754-2008 β€œbinary64” floating point number nearest to decimal value
    • \(0.3 \approx \left(1.0011001100110011001100110011001100110011001100110011\right)_2 \times 2^{-2}\) \(= 0.299999999999999988897769753748434595763683319091796875\)
    • \(0.1 \approx \left(1.1001100110011001100110011001100110011001100110011010\right)_2 \times 2^{-4}\) \(= 0.1000000000000000055511151231257827021181583404541015625\)

IEEE 754

  • Because the computer calculates with the binary representation \(0.3 - 0.1\) is actually \(0.299999999999999988897769753748434595763683319091796875\) \(- 0.1000000000000000055511151231257827021181583404541015625\)

It is good to be aware of the properties of floating point numbers when computing professionally with them –– make yourself familiar with the standard and remember that you are making computations with a fixed set of rational numbers, which become more sparse the larger their absolute values are.

(More in the reading material)

88 bits as a text

0100111101101011011000010111100100101100001000000110100001110101011011010110000101101110

Divided in 8-bit groups and interpreted according to Unicode (UTF-8) will give us:

Okay, human

32 bits as an Internet address

10000010111010011110000011000100

Interpreted according to the Internet standard RFC 791 (and RFC 796), this is a class B Internet Protocol address (IPv4).

Divided into groups of 8 bits, each of which is separately converted to decimal, we get the (perhaps) more familiar form to humans:

130.233.224.196

And if we do a reverse lookup in the Domain Name System (DNS) we get

kosh.aalto.fi

Boolean operations on words

boolean-word-ops.png

Bit operations (by convention) work independently on each bit position of a word.

Forcing and toggling the value of a word

boolean-word-force-toggle.png

Shifting bits in a word

word-shift.png

Hexadecimal numbers

  • Base 16 , digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  • Basic knowledge for a programmer
    • Easy to translate to and from binary representation
    • Compact (and unique) representation
  • Four bits make one hexadecimal digit
  • Words are generally multiples of 4 (e.g. 16,32,64)

Binary Hexadecimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

Hexadecimal numbers - example

bin-to-hex.png

Hexadecimal in IPv6 - example

RFC 2373 uses 128 bits for internet addresses. These are usually written as groups of 8 groups hexadecimal numbers between 0 and ffff.

2a03:4e42:0:0:0:0:0:347

is the address of www.aalto.fi.

If we write this out in the same (decimal) way as for IPv4 addresses it would be much longer:

42.4.78.66.0.0.0.0.0.0.0.0.0.0.3.71

If we write the same address as a binary string (with decorative space and word-wrap):

0010101000000100 0100111001000010 0000000000000000 0000000000000000
0000000000000000 0000000000000000 0000000000000000 0000001101000111 

In reality, IPv6 addresses are often printed even shorter by skipping the 0 values. The above: 2a04:4e42:3a::347, where :: denotes a series of left out 0.

Hexadecimal literals

  • Most programming languages that support hexadecimal literals prefix them by 0x.
  • For example: 0x62A9 for the hexadecimal \(62A9\)

        scala> val a = 0x62A9
        val a: Int = 25257
    
  • Note that a is an Int and that it is printed in the Scala REPL as a decimal number.
  • We can print it's hexadecimal representation (including the '0x' part) by a formatted string

        scala> print("0x%X".format(a))
        0x62A9
    

    (The %X means "take the input to format and represent it as a hexadecimal integer")

  • Some languages also support literals in other bases, most commonly
    • Octal (base 8) with the prefix 0o
    • Binary with the prefix 0b

Example: Checking bit at specific position using Scala

  • Shift the bit right until the least significant position
  • Use bit-wise and with the number 1, if the result is 1 then the bit must be set
scala> val x = 0x3A // 111010 in binary
val x: Int = 58

scala> (x >> 0) & 1 // value of bit at position 0
val res0: Int = 0

scala> (x >> 1) & 1 // value of bit at position 1
val res1: Int = 1

Could also make a (silly) function:

def isOn(x:Int, j:Int) =
  if ((x>>>j) & 1) == 1
  then println(s"Bit $j is ON in $x")
  else println(s"Bit $j is OFF in $x")

Example: Creating a word with 1 at some position and 0 elsewhere

  • It is sometimes useful to create a word with zeroes everywhere except in some position j
  • This can be done by left shifting 1 the required number of steps
  • Mind the type (1 is 32 bits, 1L is 64 bits)

scala> val j = 3 // Index 3
val j: Int = 3
scala> val a = 1<<j  // Take a 1 and shift it left the required number of steps
val a: Int = 8

scala> val j = 55 // Index 55
val j: Int = 55
scala> val c = 1L<<j // If you need 64 bits use a Long integer
val c: Long = 36028797018963968

In the notes: witnessing the bits, in Scala

  • Make use of the tellAbout… functions in the reading material
  • Prints binary representation of basic Scala datatypes and strings
  • Copy-n-paste definitions to the Scala REPL, or to code
  • Could be useful when debugging exercises

  • tellAboutByte
  • tellAboutShort
  • tellAboutInt
  • tellAboutLong
  • tellAboutChar
  • tellAboutString
  • tellAboutFloat
  • tellAboutDouble

But what if I need to do operations that result in very large numbers?

  • Scala has a data structure called BigInt that can handle arbitrary integers

        scala> def test(m: BigInt) = (m * (m + 1))/2
        def test(m: BigInt): BigInt
    
        scala> test(50000000000L)
        val res0: BigInt = 1250000000025000000000
    
  • Similar structures available in other languages
  • Important: Work needed to do arithmetic operations is now potentially dependent on the number stored
    • More on this in round 7 of the course

Orders of magnitude

  • The amount of data can also be measured in bytes
    • 1 byte = 8 bits
  • It is customary to use the prefixes of the International System of Units (kilo, mega, giga, et c) to denote the orders of magnitude
  • Magnitudes are typically given either as base 10 magnitudes or base 2 magnitudes

Prefixes

Base 10 prefixes

kilo k \(10^{3} = 1000\)
mega M \(10^{6} = 1000000\)
giga G \(10^{9} = 1000000000\)
tera T \(10^{12} = 1000000000000\)
peta P \(10^{15} = 1000000000000000\)
exa E \(10^{18} = 1000000000000000000\)
zetta Z \(10^{21} = 1000000000000000000000\)
yotta Y \(10^{24} = 1000000000000000000000000\)

Base 2 prefixes

kibi Ki \(2^{10} = 1024\)
mebi Mi \(2^{20} = 1048576\)
gibi Gi \(2^{30} = 1073741824\)
tebi Ti \(2^{40} = 1099511627776\)
pebi Pi \(2^{50} = 1125899906842624\)
exbi Ei \(2^{60} = 1152921504606846976\)
zebi Zi \(2^{70} = 1180591620717411303424\)
yobi Yi \(2^{80} = 1208925819614629174706176\)

[See the ISO 80000-1 standard]

Note the discrepancy at 'higher' magnitudes - it is important to be aware which ones is intended.

For the exercises this week

  • Use the boolean operators in Scala
  • Think of Int, Long, et c. as sequences of bits
  • Remember that the least significant bit is the rightmost one
  • Know how to shift, check, and set bits