Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if int32_t number can fit in int8_t and int16_t?

Tags:

c++

c

clang

I have a int32_t number and i need to check if will it fit in int8 and in16 but when i try:

    if (number ==(int32_t)((int8_t) number)) {
    printf("%s %d\n","8", number);
    
} else if (number ==(int32_t)((int16_t) number)){
    printf("%s %d\n","16", number);
    

Compiler says that first if statement is always true, but i don't know how to do it in other way, using only "==" and bits operations like << >>. and <stdint.h>.

like image 325
Michael Palich Terentev Avatar asked Dec 06 '22 08:12

Michael Palich Terentev


1 Answers

As of C++ 20 you can do this:

std::in_range<std::int8_t>(-1)

see here

like image 177
Reinhold Avatar answered Dec 30 '22 21:12

Reinhold