Basic Data Types
Tags: beginners, algorithms, datastructures
Published: 2023-12-08 00:35:41

Basic data types are the fundamental building blocks CPUs use to represent numeric values, text, pictures, videos, speech, 3D models, and more. Although these data are organized differently, they are composed of various basic data types. They form the lowest level of data that programming languages can directly operate on.

## Table Of Contents

  * [A real-life example](#chapter-1)
  * [Common Types](#chapter-2)
  * [Representation in Binary](#chapter-3)
  * [Type Sizes and Ranges](#chapter-4)
  * [Relationship to Data Structures](#chapter-5)

## A real-life example <a name="chapter-1"></a>

**The Shipment Journey**

Every package is assigned a unique tracking number for identification. As this number can be `long`, companies use `integer` data types like `long` to store billions of IDs without size limitations.

The carrier provides estimated delivery dates, sometimes with partial day granularity. Dates contain both date and time info, making floating point data types suitable to represent complex timestamps down to the minute.

**Status Updates**

As your package travels, the system logs status messages like "Left Sort Facility" at each location. These short text updates are easily stored using `string` data types.

Heavier items require special handling and are subject to additional fees. To track weights precisely, the `float` data type captures decimal values like "4.56 lbs".

**Delivery Details**

Your driver marks the package as delivered by getting a signature. Boolean data types store true/false values to flag a delivery as complete.

Different service speeds offer varying delivery promises. Enumeration string types classify options as "Standard", "Expedited" etc.

Data types serve many purposes in real life. How it can be stored on the computer?

## Common Types <a name="chapter-2"></a>

**Basic data types are types that the CPU can directly perform operations**

- Integer types (`byte`, `short`, `int`, `long`) for whole numbers
- Floating point types (`float`, `double`) for decimal numbers
- Character type (`char`) for single-character text
- Boolean type (`bool`) for true/false values

## Representation in Binary <a name="chapter-3"></a>

**Basic data types are stored in computers in binary form**

Inside computers, all data is stored as bits - the basic units of 1s and 0s. Data types determine how many bits are used to represent each value.

For example:
- 1 `byte` typically uses 8 bits and can store values from 0 to 255 ({% katex inline %} 2^8 {% endkatex %} numbers). Multiple bytes are used for larger integer ranges.
- 1 `integer` occupied 4 bytes = 32 bits, represent {% katex inline %} 2^{32} {% endkatex %} numbers.

## Type Sizes and Ranges <a name="chapter-4"></a>

The number of bits allocated for a type determines its possible value range. Larger types can hold bigger numbers but take up more memory. Value ranges also depend on the programming language.

For illustration, I've included a table showing common type sizes and value ranges for the Java programming language. These serve as a general guide but can vary between languages and platforms.

| type | symbol | space | minimum | maximum | default |
|:----:|:------:|:-----:|:-------:|:-------:|:-------:|
| integer | `byte` | 1 byte | {% katex inline %} -2^7 {% endkatex %} | {% katex inline %} 2^7 - 1 {% endkatex %} | 0 |
| | `short` | 2 bytes | {% katex inline %} -2^{15} {% endkatex %} | {% katex inline %} 2^{15} - 1 {% endkatex %} | 0 |
| | `int` | 4 bytes | {% katex inline %} -2^{31} {% endkatex %} | {% katex inline %} 2^{31} - 1 {% endkatex %} | 0 |
| | `long` | 8 bytes | {% katex inline %} -2^{63} {% endkatex %} | {% katex inline %} 2^{63} - 1 {% endkatex %} | 0 |
| floating point number | `float` | 4 bytes | {% katex inline %} 1.175 * 10^{-38} {% endkatex %} | {% katex inline %} 3.403 * 10^{38} {% endkatex %} | 0.0f |
| | `double` | 8 bytes | {% katex inline %} 2.225 * 10^{-308} {% endkatex %} | {% katex inline %} 1.798 * 10^{308} {% endkatex %} | 0.0 |
| character | `char` | 2 bytes | {% katex inline %} 0 {% endkatex %} | {% katex inline %} 2^{16} - 1 {% endkatex %} | 0 |
| boolean | `bool` | 1 byte | {% katex inline %} false {% endkatex %} | {% katex inline %} true {% endkatex %} | false |

## Relationship to Data Structures <a name="chapter-5"></a>

Data types indicate the type of primitive content, while data structures define how that content is organized in memory. We can store different data types like integers, floats, and chars using the same structures as arrays.

Structures provide the logical “container” for values, independent of their basic format. This relationship allows flexibility in solving problems by decoupling logical structure from physical representation.

python code:

```python
numbers: list[int] = [0] * 2
decimals: list[float] = [0.0] * 2
characters: list[str] = ['0'] * 2
bools: list[bool] = [False] * 2
data = [1, 0.1, '0', False]
```

javascript code:

```javascript
const array = [0, 0.0, 'a', false];
```

typescript code:

```typescript
const numbers: number[] = [];
const characters: string[] = [];
const bools: boolean[] = [];
```