C++ : why bool is 8 bits long?

Because every C++ data type must be addressable. How would you create a pointer to a single bit? You can’t. But you can create a pointer to a byte. So a boolean in C++ is typically byte-sized. (It may be larger as well. That’s up to the implementation. The main thing is that it must … Read more

Is the operation “false < true" well defined?

TL;DR: The operations are well defined according to the draft C++ standard. Details We can see that by going to the draft C++ standard section 5.9 Relational operators which says (emphasis mine going forward): The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. The operators < (less than), > (greater than), <= … Read more

Why does “a == x or y or z” always evaluate to True? How can I compare “a” to all of those?

In many cases, Python looks and behaves like natural English, but this is one case where that abstraction fails. People can use context clues to determine that “Jon” and “Inbar” are objects joined to the verb “equals”, but the Python interpreter is more literal minded. if name == “Kevin” or “Jon” or “Inbar”: is logically … Read more

Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?

I believe there is a difference between bool and BOOL, check out this webpage for an explanation of why: http://iosdevelopertips.com/objective-c/of-bool-and-yes.html Because BOOL is an unsigned char rather than a primitive type, variables of type BOOL can contain values other than YES and NO. Consider this code: BOOL b = 42; if (b) { printf(“b is … Read more

When should null values of Boolean be used?

Use boolean rather than Boolean every time you can. This will avoid many NullPointerExceptions and make your code more robust. Boolean is useful, for example to store booleans in a collection (List, Map, etc.) to represent a nullable boolean (coming from a nullable boolean column in a database, for example). The null value might mean … Read more

Ruby: How to convert a string to boolean

If you use Rails 5, you can do ActiveModel::Type::Boolean.new.cast(value). In Rails 4.2, use ActiveRecord::Type::Boolean.new.type_cast_from_user(value). The behavior is slightly different, as in Rails 4.2, the true value and false values are checked. In Rails 5, only false values are checked – unless the values is nil or matches a false value, it is assumed to be … Read more