Questions tagged [boolean]

A Boolean data type is a data type with only two possible values: true or false.

The Boolean data type represents the simple True or False values of Boolean algebra.

Though one bit is all that is necessary to store a boolean (the 1 = true, 0 = false), it is often advantageous (if unintuitively so) to use larger amounts of memory.

Some programming languages actually define a three-valued logic within their Boolean type.

In , the Boolean class is java.lang.Boolean, the object version of primitive type boolean, and questions may be related to either. Note that the java.lang.Boolean fields may hold null as well, as it's an Object.

11685 questions
252
votes
10 answers

JS generate random boolean

Simple question, but I'm interested in the nuances here. I'm generating random booleans using the following method I came up with myself: const rand = Boolean(Math.round(Math.random())); Whenever random() shows up, it seems there's always a pitfall…
Ben
  • 54,723
  • 49
  • 178
  • 224
251
votes
6 answers

bash "if [ false ];" returns true instead of false -- why?

Why does the following output True? #!/bin/sh if [ false ]; then echo "True" else echo "False" fi This will always output True even though the condition would seem to indicate otherwise. If I remove the brackets [] then it works, but I do…
tenmiles
  • 2,521
  • 3
  • 18
  • 20
243
votes
9 answers

How do I use boolean variables in Perl?

I have tried: $var = false; $var = FALSE; $var = False; None of these work. I get the error message Bareword "false" not allowed while "strict subs" is in use.
Chad DeShon
  • 4,732
  • 6
  • 28
  • 29
242
votes
6 answers

How to count the number of true elements in a NumPy bool array

I have a NumPy array 'boolarr' of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for this task? Or, do I need to iterate over the elements in my script?
norio
  • 3,652
  • 3
  • 25
  • 33
236
votes
9 answers

Cleanest way to toggle a boolean variable in Java?

Is there a better way to negate a boolean in Java than a simple if-else? if (theBoolean) { theBoolean = false; } else { theBoolean = true; }
Kevin Griffin
  • 14,084
  • 7
  • 28
  • 23
219
votes
15 answers

What is the difference between & and && in Java?

I always thought that && operator in Java is used for verifying whether both its boolean operands are true, and the & operator is used to do Bit-wise operations on two integer types. Recently I came to know that & operator can also be used verify…
Lavneesh
215
votes
14 answers

PHP - Get bool to echo false when false

The following code doesn't print out anything: $bool_val = (bool)false; echo $bool_val; But the following code prints 1: $bool_val = (bool)true; echo $bool_val; Is there a better way to print 0 or false when $bool_val is false than adding an if…
Anonymous1
  • 3,877
  • 3
  • 28
  • 42
209
votes
13 answers

Why is a boolean 1 byte and not 1 bit of size?

In C++, Why is a boolean 1 byte and not 1 bit of size? Why aren't there types like a 4-bit or 2-bit integers? I'm missing out the above things when writing an emulator for a CPU
Asm
  • 2,101
  • 2
  • 13
  • 4
208
votes
3 answers

Converting string "true" / "false" to boolean value

I have a JavaScript string containing "true" or "false". How may I convert it to boolean without using the eval function?
user160820
  • 14,866
  • 22
  • 67
  • 94
206
votes
9 answers

Return Boolean Value on SQL Select Statement

How to return a boolean value on SQL Select Statement? I tried this code: SELECT CAST(1 AS BIT) AS Expr1 FROM [User] WHERE (UserID = 20070022) And it only returns TRUE if the UserID exists on the table. I want it to return FALSE if the UserID…
mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
201
votes
8 answers

Counting the number of True Booleans in a Python List

I have a list of Booleans: [True, True, False, False, False, True] and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3.) I have found examples of looking for the number of…
acs
  • 2,089
  • 3
  • 15
  • 10
199
votes
10 answers

Objective-C : BOOL vs bool

I saw the "new type" BOOL (YES, NO). I read that this type is almost like a char. For testing I did : NSLog(@"Size of BOOL %d", sizeof(BOOL)); NSLog(@"Size of bool %d", sizeof(bool)); Good to see that both logs display "1" (sometimes in C++ bool is…
Francescu
  • 16,974
  • 6
  • 49
  • 60
195
votes
5 answers

Is there an XNOR (Logical biconditional) operator in C#?

I could not find an XNOR operator to provide this truth table: a b a XNOR b ---------------- T T T T F F F T F F F T Is there a specific operator for this? Or I need to use !(A^B)?
trailmax
  • 34,305
  • 22
  • 140
  • 234
195
votes
8 answers

How do I get the opposite (negation) of a Boolean in Python?

For the following sample: def fuctionName(int, bool): if int in range(...): if bool == True: return False else: return True Is there any way to skip the second if-statement? Just to tell the computer to…
amyassin
  • 2,714
  • 3
  • 25
  • 32
194
votes
6 answers

How are booleans formatted in Strings in Python?

I see I can't do: "%b %b" % (True, False) in Python. I guessed %b for b(oolean). Is there something like this?
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133