Data Types
Data types reference explaining numeric, text, Boolean, and composite values, binary representation, storage, type selection for programming and engineering.
By Merrick S
Overview
A data type fixes how many bits a value occupies and how those bits are interpreted. The C/C++ integer types come in signed and unsigned variants at 8, 16 and 32 bits, and each doubling of width squares the number of representable values. Signed integers use two's complement, so half the range lies below zero. Floating-point types follow IEEE 754 and trade exact integer range for a much wider exponent range.
Data Types and Value Ranges
| Unsigned char | 8 | 0 to 255 |
| Signed char | 8 | -128 to 127 |
| Unsigned short | 16 | 0 to 65535 |
| Signed short | 16 | -32768 to 32767 |
| Unsigned int | 32 | 0 to 4294967295 |
| Signed int | 32 | -2147483648 to 2147483647 |
| Float | 32 | -3.4028×10^38 to 3.4028×10^38 |
| Double | 64 | -1.7977×10^308 to 1.7977×10^308 |
Source: engineeringtoolbox.com
Binary Representation and Examples
Understanding data types involves how values are encoded in binary. Key concepts include:
- Byte: One byte is equivalent to 8 bits.
- Signed vs. unsigned: For signed types, the most significant bit (MSB) indicates the sign: 0 for positive, 1 for negative. Unsigned types only represent non-negative values, using all bits for magnitude.
- Two's complement: Signed integers typically use two's complement representation, where negative numbers are derived by inverting bits and adding one.
Illustrative examples:
- Unsigned char (8 bits): The binary
01100100represents 100 in decimal. - Signed char (8 bits): The binary
01100100is 100, while10011100represents -100 using two's complement. - Larger types: For short, int, float and double, similar principles apply with extended bit patterns. Float and double follow IEEE 754 standards for floating-point encoding, as seen in their binary representations for values like 100 and -100.
For precise binary conversions, tools like binary-to-decimal converters can be helpful.