c++で各整数型の最大値
c++整数型の最大値を計算するマクロ。を書いたのでメモ。
#include<iostream>
using namespace std;
#define TYPEMAX(type) { \
const unsigned long long temp = (1llu << (sizeof(type)*8 - 1)) - 1; \
cout << #type << ": " << temp << endl; \
}
#define UTYPEMAX(type) { \
const unsigned long long temp1 = (1llu << (sizeof(type)*8 - 1)) - 1; \
const unsigned long long temp2 = (1llu << (sizeof(type)*8 - 1)); \
cout << #type << ": " << temp1+temp2 << endl; \
}
int main(){
TYPEMAX(char);
TYPEMAX(int);
TYPEMAX(long long);
UTYPEMAX(unsigned char);
UTYPEMAX(unsigned int);
UTYPEMAX(unsigned long long);
}
char: 127
int: 2147483647
long long: 9223372036854775807
unsigned char: 255
unsigned int: 4294967295
unsigned long long: 18446744073709551615