In computers, data is stored in bits, the smallest unit of information. A bit can represent two states: 0 or 1. When we group bits, we form bytes, and the sizes of C++ data types are often expressed in bytes.
Bits and Binary Representation:
In the binary system, each bit represents a binary digit, either 1 or 0 The number of bits in a data type directly influences its capacity to represent different values. The formula for calculating the capacity is 2 ^ n
, where n is the number of bits. This stems from the face that each bit has two possible values (0 or 1).
Unsigned Representation:
Unsigned integers represent only non-negative values. The range for an n-bit unsigned integer is 0 to 2^(n-1)
, where n is the number of bits. For example, an 8-bit unsigned integer can represent values from 0 to 255.
- If the value exceeds the maximum limit, it wraps around to 0.
Below is the 16 possible values of a four-bit unsigned int:
bits value
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
and here are the 16 possible values of a four-bit signed int
bits value
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 -8
1001 -7
1010 -6
1011 -5
1100 -4
1101 -3
1110 -2
1111 -1
1. Char (Character) Data Type:
- Size: 1 byte
- Capacity: 256 possible values (2^8)
- Range: 0 to 255 (unsigned) or -128 to 127 (signed)
2. Integer Data Types:
Short Integer (short
):
- Size: Usually 2 bytes.
- Capacity: 65536 possible values (2^16)
- Range: -32,768 to 32,767 (signed) or 0 to 65,535 (unsigned)
Integer (int
):
- Size: Typically 4 bytes
- Capacity: 4,294,967,296 possible values (2^32)
- Range: -2,147,483,648 to 2,147,483,647 (signed) or 0 to 4,294,967,295 (unsigned)
Long Integer (long
):
- Size: Typically 4 bytes
- Capacity:
Long Long Integer (long long
):
- Size: Usually 8 bytes.
- Capacity: 18,446,744,073,709,551,616 possible values (2^64)
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed) or 0 to 18,446,744,073,709,551,615 (unsigned).
3. Floating-Point Data Types:
Float:
- Size: Typically 4 bytes.
Double:
- Size: Usually 8 bytes.
Long Double:
- Size: The size can vary.
4. Boolean Data Type:
- Size: 1 byte
- Capacity: 2 possible value (2)
- Range: 0 (false) or 1 (true)