lundi 22 février 2016

Is there a standardized maximum variable binary width that an if statement can evaluate?

Is the maximum variable width that an if statement can evaluate implementation specific. Or can a compiler force a cast on a variable to a another type such as a boolean to be evaluated by an if statement. Consider the following code below, as well as the output on my machine. I ask because I would like to reliably distribute a library using for use on a number of different machines including arm, mips, amd86, x86 etc. Please note that I understand that native integer types do not always reach 64/32/16 bit width.

Furthermore if it is defined, then how exactly is it defined?

Code:

#include <iostream>
#include <cstdint>
#include <bitset>
int main() {

// Initialises variables with a single binary 1 in the left most place.
uint8_t eight_bit = (~0) << (8 - 1);
uint16_t sixteen_bit = (~0) << (16 - 1);
uint32_t thirty_two_bit = (~0) << (32 - 1);
uint64_t sixty_four_bit = (~0) << (64 - 1);


std::cout << "The 8 bit integer is equal to: " 
    << std::bitset<8>(eight_bit) << "\n";
std::cout << "The 16 bit integer is equal to: " 
    << std::bitset<16>(sixteen_bit) << "\n";
std::cout << "The 32 bit integer is equal to: " 
    << std::bitset<32>(thirty_two_bit) << "\n";
std::cout << "The 64 bit integer is equal to: " 
    << std::bitset<64>(sixty_four_bit) << "\n";


if (eight_bit)      
    std::cout << "8 bit variable considered true." << "\n";

if (sixteen_bit)    
    std::cout << "16 bit variable considered true." << "\n";

if (thirty_two_bit) 
    std::cout << "32 bit variable considered true." << "\n";

if (sixty_four_bit) 
    std::cout << "64 bit variable considered true." << "\n";

return 0;
}

Output:

The 8 bit integer is equal to: 10000000
The 16 bit integer is equal to: 1000000000000000
The 32 bit integer is equal to: 10000000000000000000000000000000
The 64 bit integer is equal to: 1000000000000000000000000000000000000000000000000000000000000000
8 bit variable considered true.
16 bit variable considered true.
32 bit variable considered true.
64 bit variable considered true.

Aucun commentaire:

Enregistrer un commentaire