Chad Harrison
Chad Harrison

Reputation: 2868

What datatype does the result of a binary comparison reduce to?

Relizing there's no such thing as a BOOL datatype, take the following:

std::cout << (1>2); //<<-- prints 0

Assuming this false comparison is a 0, what datatype deos the result of a comparison reduce to? Doing a quick google search doesn't yield any results. My best guess it that it's an unsigned char because it's the smallest most basic datatype where 0 truly represented as 0x00. I don't want to assume anything because I'm not sure what voodoo std::cout does to the value to make it a printable character.

Upvotes: 0

Views: 151

Answers (4)

Abdurahman
Abdurahman

Reputation: 658

This is not a direct answer to your question:

as said here it is bool in c++ and int in c but you i think the part which you would think of is how much memory it takes to save a comparison result ?

  • as you know a data type defines how much memory to be allocated.

  • Note: sometimes it(data type to memory size definition) is different from compiler/processor architecture to another for example in embedded systems environment people used to talk about and define data types in projects using number of bits e.g typedef unsigned char uint8; instead of using standard data types directly so it would be easy to port to another compiler/target processor

  • you should look at this: Why is a char and a bool the same size in c++?

  • you should look at this also http://www.cplusplus.com/doc/tutorial/variables under "Fundamental data types" section a table of each data type and its size and range but he noted :

The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems. But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

  • as noted by sftrabbit about the standard in his answer that the standard is abstract enough and not detailed thus i think that the size of bool in memory is an implementation specific which may be different from c++ compiler to another , check Nawazs' answer here: How a bool type variable is stored in memory? (C++)

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110768

The type of the result of all relational operators (<, >, <=, >=) is bool:

The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool.

An object of type bool has the values true or false.Under integral promotion, a bool can be converted to an int where false becomes 0 and true becomes 1:

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

bool is an integral type, which the standard says are represented by use of a "pure binary numeration system". The footnote that describes this representation is fairly unclear as to how it maps to the values true and false, but you could assume that they are implying that the value representation for 0 would be all 0 bits:

A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)

Upvotes: 10

Carl Norum
Carl Norum

Reputation: 225262

The C++ standard, section 5.9 Relational operators, paragraph 1 says:

The type of the result is bool.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

There's no standard BOOL type, but bool is a standard fundamental type:

[C++11: 3.9.1/6]: Values of type bool are either true or false. [..]

As for the result of your relational comparison:

[C++11: 5.9/1]: The relational operators group left-to-right. [..] The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool.

Note that this is not the same in C, in which there is no built-in type bool and the result of relational comparisons is of type int:

[C99: 6.5/8]: Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

Upvotes: 6

Related Questions