How to chcek if a variable is “False” in Django templates?

For posterity, I have a few NullBooleanFields and here’s what I do: To check if it’s True: {% if variable %}True{% endif %} To check if it’s False (note this works because there’s only 3 values — True/False/None): {% if variable != None %}False{% endif %} To check if it’s None: {% if variable == … Read more

Converting bool to text in C++

How about using the C++ language itself? bool t = true; bool f = false; std::cout << std::noboolalpha << t << ” == ” << std::boolalpha << t << std::endl; std::cout << std::noboolalpha << f << ” == ” << std::boolalpha << f << std::endl; UPDATE: If you want more than 4 lines of code … Read more

Setting extra bits in a bool makes it true and false at the same time

In C++ the bit representation (and even the size) of a bool is implementation defined; generally it’s implemented as a char-sized type taking 1 or 0 as possible values. If you set its value to anything different from the allowed ones (in this specific case by aliasing a bool through a char and modifying its … Read more

PHP printed boolean value is empty, why?

Be careful when you convert back and forth with boolean, the manual says: A boolean TRUE value is converted to the string “1”. Boolean FALSE is converted to “” (the empty string). This allows conversion back and forth between boolean and string values. So you need to do a: echo (int)$local_rates_file_exists.”<br>”;

What is performance-wise the best way to generate random bools?

For the purpose of performance, at a price of less “randomness” than e.g. std::mt19937_64, you can use Xorshift+ to generate 64-bit numbers and then use the bits of those numbers as pseudo-random booleans. Quoting the Wikipedia: This generator is one of the fastest generators passing BigCrush Details: http://xorshift.di.unimi.it/ . There is a comparison table in … Read more