Questions tagged [boolean]
A Boolean data type is a data type with only two possible values: true or false.
boolean
11,755
questions
2131
votes
21
answers
3.2m
views
How to check if the string is empty in Python?
Does Python have something like an empty string variable where you can do:
if myString == string.empty:
Regardless, what's the most elegant way to check for empty string values? I find hard coding &...
1473
votes
27
answers
1.7m
views
How can I declare and use Boolean variables in a shell script?
I tried to declare a Boolean variable in a shell script using the following syntax:
variable=$false
variable=$true
Is this correct? Also, if I wanted to update that variable would I use the same ...
1387
votes
13
answers
1.1m
views
Which MySQL data type to use for storing boolean values
Since MySQL doesn't seem to have any 'boolean' data type, which data type do you 'abuse' for storing true/false information in MySQL?
Especially in the context of writing and reading from/to a PHP ...
1267
votes
40
answers
1.3m
views
Converting from a string to boolean in Python
How do I convert a string into a boolean in Python? This attempt returns True:
>>> bool("False")
True
1097
votes
28
answers
896k
views
Parsing boolean values with argparse
I would like to use argparse to parse boolean command-line arguments written as "--foo True" or "--foo False". For example:
my_program --my_boolean_flag False
However, the following test code does ...
864
votes
19
answers
1.6m
views
Using Boolean values in C
C doesn't have any built-in Boolean types. What's the best way to use them in C?
853
votes
14
answers
2.4m
views
Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
I want to filter my dataframe with an or condition to keep rows with a particular column's values that are outside the range [-0.25, 0.25]. I tried:
df = df[(df['col'] < -0.25) or (df['col'] > 0....
697
votes
8
answers
1.3m
views
What is the printf format specifier for bool?
Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool?
I mean something like in that pseudo code:
bool x = true;
printf("%B\n", x);
which would ...
615
votes
65
answers
202k
views
Check if at least two out of three booleans are true
An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true.
My solution follows:
boolean atLeastTwo(boolean a, ...
551
votes
9
answers
442k
views
How to toggle a boolean?
Is there a really easy way to toggle a boolean value in javascript?
So far, the best I've got outside of writing a custom function is the ternary:
bool = bool ? false : true;
463
votes
20
answers
479k
views
How to convert string to boolean php
How can I convert string to boolean?
$string = 'false';
$test_mode_mail = settype($string, 'boolean');
var_dump($test_mode_mail);
if($test_mode_mail) echo 'test mode is on.';
it returns,
...
450
votes
9
answers
996k
views
Is there a Boolean data type in Microsoft SQL Server like there is in MySQL? [duplicate]
Is there a Boolean data type in Microsoft SQL Server like there is in MySQL?
If not, what is the alternative in MS SQL Server?
449
votes
22
answers
399k
views
Convert boolean result into number/integer
I have a variable that stores false or true, but I need 0 or 1 instead, respectively. How can I do this?
420
votes
12
answers
574k
views
Convert boolean to int in Java
What is the most accepted way to convert a boolean to an int in Java?
416
votes
13
answers
1.1m
views
How to create a yes/no boolean field in SQL server?
What is the best practice for creating a yes/no i.e. Boolean field when converting from an access database or in general?
396
votes
15
answers
229k
views
What is the difference between bool and Boolean types in C#
What is the difference between bool and Boolean types in C#?
368
votes
16
answers
668k
views
How to convert String object to Boolean Object?
How to convert String object to Boolean object?
341
votes
11
answers
546k
views
Is there any boolean type in Oracle databases?
Is there any Boolean type in Oracle databases, similar to the BIT datatype in Ms SQL Server?
331
votes
8
answers
544k
views
Default value of 'boolean' and 'Boolean' in Java
What are the default values of boolean (primitive) and Boolean (primitive wrapper) in Java?
311
votes
12
answers
404k
views
Is bool a native C type?
I've noticed that the Linux kernel code uses bool, but I thought that bool was a C++ type. Is bool a standard C extension (e.g., ISO C90) or a GCC extension?
309
votes
11
answers
327k
views
Returning a boolean from a Bash function
I want to write a bash function that check if a file has certain properties and returns true or false. Then I can use it in my scripts in the "if". But what should I return?
function myfun(){ ... ...
306
votes
6
answers
24k
views
Strange definitions of TRUE and FALSE macros
I have seen the following macro definitions in a coding book.
#define TRUE '/'/'/'
#define FALSE '-'-'-'
There was no explanation there.
Please explain to me how these will work as TRUE and FALSE.
304
votes
12
answers
136k
views
Volatile boolean vs AtomicBoolean
What does AtomicBoolean do that a volatile boolean cannot achieve?
300
votes
13
answers
403k
views
How can I map True/False to 1/0 in a Pandas DataFrame?
I have a column in python pandas DataFrame that has boolean True/False values, but for further calculations I need 1/0 representation. Is there a quick pandas/numpy way to do that?
300
votes
7
answers
405k
views
How do I create a numpy array of all True or all False?
In Python, how do I create a numpy array of arbitrary shape filled with all True or all False?
300
votes
4
answers
336k
views
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
Is it guaranteed that False == 0 and True == 1, in Python (assuming that they are not reassigned by the user)? For instance, is it in any way guaranteed that the following code will always produce ...
298
votes
4
answers
502k
views
Logical operators for Boolean indexing in Pandas
I'm working with a Boolean index in Pandas.
The question is why the statement:
a[(a['some_column']==some_number) & (a['some_other_column']==some_other_number)]
works fine whereas
a[(a['...
290
votes
15
answers
290k
views
In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?
The following shows that "0" is false in Javascript:
>>> "0" == false
true
>>> false == "0"
true
So why does the following print "ha"?
>>> if ("0") console.log("ha")
ha
276
votes
7
answers
146k
views
Why does Boolean.ToString output "True" and not "true"
true.ToString()
false.toString();
Output:
True
False
Is there a valid reason for it being "True" and not "true"? It breaks when writing XML as XML's boolean type is lower case, and also isn't ...
263
votes
10
answers
197k
views
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 ...
263
votes
8
answers
436k
views
Boolean vs boolean in Java
There are discussions around Integer vs int in Java. The default value of the former is null while in the latter it's 0. How about Boolean vs boolean?
A variable in my application can have 0/1 ...
259
votes
6
answers
319k
views
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 ...
250
votes
6
answers
345k
views
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 ...
244
votes
10
answers
273k
views
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.
237
votes
10
answers
202k
views
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;
}
221
votes
15
answers
246k
views
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.
...
217
votes
14
answers
218k
views
PHP - Get bool to echo false when false [duplicate]
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 ...
215
votes
13
answers
106k
views
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
212
votes
9
answers
488k
views
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 ...
212
votes
3
answers
320k
views
Converting string "true" / "false" to boolean value [duplicate]
I have a JavaScript string containing "true" or "false".
How may I convert it to boolean without using the eval function?
211
votes
8
answers
427k
views
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 ...
209
votes
8
answers
261k
views
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 ...
205
votes
3
answers
129k
views
Boolean literals in PowerShell
What are the Boolean literals in PowerShell?
202
votes
6
answers
184k
views
How to print boolean value in Go?
As we have %d for int. What is the format specifier for boolean values?
201
votes
11
answers
204k
views
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 ...
198
votes
5
answers
79k
views
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 ...
195
votes
6
answers
219k
views
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?
193
votes
6
answers
416k
views
How to convert a boolean array to an int array
I use Scilab, and want to convert an array of booleans into an array of integers:
>>> x = np.array([4, 3, 2, 1])
>>> y = 2 >= x
>>> y
array([False, False, True, True],...
188
votes
17
answers
243k
views
Ruby: How to convert a string to boolean
I have a value that will be one of four things: boolean true, boolean false, the string "true", or the string "false". I want to convert the string to a boolean if it is a string, otherwise leave it ...
178
votes
8
answers
47k
views
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
I am writing a security system that denies access to unauthorized users.
name = input("Hello. Please enter your name: ")
if name == "Kevin" or "Jon" or "Inbar":
...