CS-A1120 Programming 2
Department of Computer Science
Aalto University
We will build this in Scala by using classes that represents logical components. In the end, working on a simulated processor.
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
Int: error for a smaller inputLong allows for larger numbersInt uses 32 bitsLong uses 64 bits
See this week's reading material for even more information!
While there has been analog computers, both historical and modern, commercial computers today are digital.
Example: 16 bit word
(Same as for decimal numbers, the least significant digit is the rightmost one.)
| 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 | = π |
Int is 32 bit, so \(2^{32}\) different statesLong is 64 bit, so \(2^{64}\) different statesThe 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}\)
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 …)
The final result is \(\left(11001\right)_2\)
not of positive), e.g.
not of positive + 1), e.g.
| 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 |
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)
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..
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
Interpretation is all about the standard / format / convention
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\).
0100000000001001001000011111101101010100010001000010110100011000
interpreted according to the IEEE Std 754 floating point standard is
\(3.141592653589793115997963468544185161590576171875\)
scala.math.Pi
Float = IEEE 754 'binary32'Double = IEEE 754 'binary64'scala> 0.3 - 0.1
Result?
val res10: Double = 0.19999999999999998
What is going on here?
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)
0100111101101011011000010111100100101100001000000110100001110101011011010110000101101110
Divided in 8-bit groups and interpreted according to Unicode (UTF-8) will give us:
Okay, human
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
Bit operations (by convention) work independently on each bit position of a word.
| 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 |
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
0 values. The above: 2a04:4e42:3a::347, where :: denotes a series of left out 0.
0x.
For example: 0x62A9 for the hexadecimal \(62A9\)
scala> val a = 0x62A9 val a: Int = 25257
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")
0o0bscala> 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")
j1 the required number of steps1L 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
tellAbout… functions in the reading material
tellAboutBytetellAboutShorttellAboutInttellAboutLongtellAboutChartellAboutStringtellAboutFloattellAboutDouble
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
| 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\) |
| 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\) |
Note the discrepancy at 'higher' magnitudes - it is important to be aware which ones is intended.
Int, Long, et c. as sequences of bits