Questions tagged [boolean]

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

boolean
Filter by
Sorted by
Tagged with
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 &...
Joan Venge's user avatar
  • 323k
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 ...
hassaanm's user avatar
  • 15.1k
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 ...
user avatar
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
Joan Venge's user avatar
  • 323k
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 ...
SuperElectric's user avatar
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?
neuromancer's user avatar
  • 54.6k
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....
obabs's user avatar
  • 8,841
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 ...
maxschlepzig's user avatar
  • 37.2k
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;
Chris Dutrow's user avatar
  • 49.6k
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, ...
Run's user avatar
  • 56k
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?
Ayyappan Anbalagan's user avatar
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?
hd.'s user avatar
  • 17.9k
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?
hpique's user avatar
  • 120k
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?
leora's user avatar
  • 192k
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#?
Gary Willoughby's user avatar
368 votes
16 answers
668k views

How to convert String object to Boolean Object?

How to convert String object to Boolean object?
Suresh's user avatar
  • 39.1k
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?
Peder's user avatar
  • 3,411
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?
Makky's user avatar
  • 17.3k
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?
asussex's user avatar
  • 3,151
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(){ ... ...
luca's user avatar
  • 12.5k
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.
Keshava GN's user avatar
  • 4,215
304 votes
12 answers
136k views

Volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve?
JeffV's user avatar
  • 53.8k
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?
Simon Righley's user avatar
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?
Michael Currie's user avatar
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 ...
Eric O. Lebigot's user avatar
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['...
user2988577's user avatar
  • 4,077
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
nonopolarity's user avatar
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 ...
Chris S's user avatar
  • 65.1k
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 ...
Ben's user avatar
  • 55.9k
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 ...
Neel's user avatar
  • 10.1k
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 ...
tenmiles's user avatar
  • 2,601
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 ...
norio's user avatar
  • 3,772
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.
Chad DeShon's user avatar
  • 4,742
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; }
Kevin Griffin's user avatar
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. ...
user avatar
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 ...
Anonymous1's user avatar
  • 3,907
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
Asm's user avatar
  • 2,161
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 ...
mrjimoy_05's user avatar
  • 3,492
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?
user160820's user avatar
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 ...
amyassin's user avatar
  • 2,883
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 ...
acs's user avatar
  • 2,169
205 votes
3 answers
129k views

Boolean literals in PowerShell

What are the Boolean literals in PowerShell?
Colonel Panic's user avatar
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?
Anuj Verma's user avatar
  • 2,669
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 ...
Francescu's user avatar
  • 17k
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 ...
trailmax's user avatar
  • 34.7k
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?
Juanjo Conti's user avatar
  • 29.3k
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],...
Kwolf's user avatar
  • 2,041
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 ...
emery's user avatar
  • 9,077
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": ...
Kevin's user avatar
  • 75.6k

1
2 3 4 5
236