Fastest way to generate a random boolean

The first option – rand.Next(2) executes behind the scenes the following code: if (maxValue < 0) { throw new ArgumentOutOfRangeException(“maxValue”, Environment.GetResourceString(“ArgumentOutOfRange_MustBePositive”, new object[] { “maxValue” })); } return (int) (this.Sample() * maxValue); and for the second option – rand.NextDouble(): return this.Sample(); Since the first option contains maxValue validation, multiplication and casting, the second option is … Read more

BOOLEAN or TINYINT confusion

MySQL does not have internal boolean data type. It uses the smallest integer data type – TINYINT. The BOOLEAN and BOOL are equivalents of TINYINT(1), because they are synonyms. Try to create this table – CREATE TABLE table1 ( column1 BOOLEAN DEFAULT NULL ); Then run SHOW CREATE TABLE, you will get this output – … Read more

Which is faster : if (bool) or if(int)?

Makes sense to me. Your compiler apparently defines a bool as an 8-bit value, and your system ABI requires it to “promote” small (< 32-bit) integer arguments to 32-bit when pushing them onto the call stack. So to compare a bool, the compiler generates code to isolate the least significant byte of the 32-bit argument … Read more

bool operator ++ and —

It comes from the history of using integer values as booleans. If x is an int, but I am using it as a boolean as per if(x)… then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it (barring overflow). However, it’s impossible to predict … Read more

Boolean.valueOf() produces NullPointerException sometimes

You’ve got to look carefully at which overload is being invoked: Boolean.valueOf(null) is invoking Boolean.valueOf(String). This doesn’t throw an NPE even if supplied with a null parameter. Boolean.valueOf(modifiedItems.get(“item1”)) is invoking Boolean.valueOf(boolean), because modifiedItems‘s values are of type Boolean, which requires an unboxing conversion. Since modifiedItems.get(“item1”) is null, it is the unboxing of that value – … Read more

How do I use a Boolean in Python?

checker = None if some_decision: checker = True if checker: # some stuff [Edit] For more information: http://docs.python.org/library/functions.html#bool Your code works too, since 1 is converted to True when necessary. Actually Python didn’t have a boolean type for a long time (as in old C), and some programmers still use integers instead of booleans.

Why is Java’s boolean primitive size not defined?

Short answer: yes, boolean values are manipulated as 32-bit entities, but arrays of booleans use 1 byte per element. Longer answer: the JVM uses a 32-bit stack cell, used to hold local variables, method arguments, and expression values. Primitives that are smaller than 1 cell are padded out, primitives larger than 32 bits (long and … Read more