Number Systems
n bits이 있으면 2^n 개의 data를 나타낼 수 있음
e.g.) 8 bits: data range는 0 부터 2^8-1 = 255, 총 256개
Digits | Bytes | Data Range |
8 | 1 | 0~255 |
16 | 2 | 0~65,535 |
32 | 4 | 0~2^32-1 = 4,294,967,295 |
64 | 8 | 0~2^64-1 |
Binary number system (base = 2, 1개의 bit으로 하나의 value를 나타냄, 1, 2)
Octal number system (base = 8, 3개의 bit으로 하나의 value를 나타냄, 0-7)
Decimal number system (base = 10)
Hexadecimal number system (base = 16, 4개의 bit으로 하나의 value를 나타냄, 0-9, A-F)
int n1 {15}; // Decimal
int n2 {017}; // Octal, 1*8^1 + 7*8^0 = 15
int n3 {0x0f}; // Hexadecimal, 0*16^1 + 15*16^0 = 15
int n4 {0b00001111}; // Binary
int
stores decimals, typically occupies 4 or more bytes in memory
variable: a named piece of memory that you use to store specific types of data
#include <iostream>
int main(){
int age1 {2.9}; // braced initialization --> compile error
int age2 (2.9); // functional initialization --> information lost
int age3 = 2.9; // assignment initialization --> information lost
std::cout << age2 << std::endl; // output 2 (chop off decimal point)
std::cout << age3 << std::endl; // output 2 (chop off decimal point)
return 0;
}
#include <iostream>
int main(){
int age {9};
std::cout << sizeof(int) << std::endl; // 4 bytes
std::cout << sizeof(age) << std::endl; // 4 bytes
return 0;
}
Type with modifier | Bytes in memory | Data range |
unsigned short int | 2 | 0~2^16-1 |
signed short int | 2 | -2^15~2^15-1 |
unsigned int | 4 | 0~2^32-1 |
signed int | 4 | -2^31~2^31-1 |
signed long int | 4 or 8 | 2^64-1 (if 8) |
unsigned long int | 4 or 8 | -2^63~2^63-1 (if 8) |
unsigned long long int | 8 | 2^64-1 |
signed long long int | 8 | -2^63~2^63-1 |
Fractional Numbers
Type | Size (Bytes in memory) | Precision | Comment |
float | 4 | 7 decimal digits | - |
double | 8 | 15-16 decimal digits | Recommended default |
long double | 8 or 16 | > double | - |
n (floating point) / 0 --> inf (+/-)
0.0 / 0.0 --> nan
#include <iostream>
#include <iomanip>
int main(){
float n1 {1.12345678901234567890f};
double n2 {1.12345678901234567890};
long double n3 {1.12345678901234567890L};
std::cout << std::setprecision(20);
std::cout << n1 << std::endl; // 1.1234568357467651367
std::cout << n2 << std::endl; // 1.1234567890123456912
std::cout << n3 << std::endl; // 1.1234567890123456912
std::cout << sizeof(float) << std::endl; // 4
std::cout << sizeof(double) << std::endl; // 8
std::cout << sizeof(long double) << std::endl; // 8
return 0;
}
Boolean
bool occupies 8 bits (1 byte) in memory.
A byte can store 256 different values. Using it just to cover two states (true/false) is wasteful, especially devices with hard memory constraints (embedded devices). There are techniques to pack even more data into a byte.
Print 하면 true --> 1, false --> 0 으로 나옴
#include <iostream>
int main(){
bool red_light {true};
bool green_light {false};
std::cout << red_light << std::endl; // 1
std::cout << green_light << std::endl; // 0
std::cout << std::boolalpha;
std::cout << red_light << std::endl; // true
std::cout << green_light << std::endl; // false
return 0;
}
Characters and Text
char occupies 8 bits (1 byte) in memory. Each value (from 256 possible values) match to some character.
#include <iostream>
int main(){
char c1 {'a'};
char c2 {65}; // ASCII character code 'A'
std::cout << c1 << std::endl; // A
std::cout << static_cast<int>(c2) << std::endl; // 65
}
Auto Keyword
auto lets the compiler to deduce the type
#include <iostream>
int main(){
auto v1 {15}; // int: 4 bytes
auto v2 {13.0}; // double: 8 bytes
auto v3 {14.0f}; // float: 4 bytes
auto v4 {15.0l}; // long double: 8 or 16 bytes
auto v5 {'c'}; // char: 1 byte
auto v6 {123u}; // unsigned int: 4 bytes
auto v7 {100ul}; // unsigned long int: 8 bytes
auto v8 {123ll}; // long long int: 8 bytes
return 0;
}
*Need to careful about auto assignments (auto keyword를 이용해서 variable 정의한 다음에 deduced type에 맞지 않는 value로 reassign 하면 compile error 없이 garbage value를 저장함)
Reference
'스터디 로그 > Computer Science' 카테고리의 다른 글
Image Processing with OpenCV and Python (0) | 2024.02.23 |
---|---|
[LeetCode] 1926. Nearest Exit from Entrance in Maze (0) | 2024.02.15 |
[C++] Lecture 1-2. C++ 시작하기 (2) | 2024.02.10 |
[Python] Bit operator (0) | 2024.02.10 |
[LeetCode] 560. Subarray Sum Equals K (1) | 2024.02.08 |