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
2080
votes
26 answers

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 "" every time for checking an empty string not as…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
1418
votes
27 answers

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 syntax? Finally, is the following syntax for using…
hassaanm
  • 14,539
  • 4
  • 21
  • 20
1372
votes
13 answers

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 script. Over time I have used and seen several…
Beat
1227
votes
39 answers

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
  • 315,713
  • 212
  • 479
  • 689
1046
votes
27 answers

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 not do what I would like: import argparse parser =…
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
857
votes
18 answers

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
  • 53,769
  • 78
  • 166
  • 223
836
votes
14 answers

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.25)] But I get the error: ValueError: The truth…
obabs
  • 8,671
  • 4
  • 12
  • 17
675
votes
8 answers

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 print: true
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
610
votes
65 answers

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, boolean b, boolean c) { if ((a && b) || (b &&…
user282886
  • 3,125
  • 8
  • 22
  • 11
544
votes
9 answers

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
  • 48,402
  • 65
  • 188
  • 258
453
votes
19 answers

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, boolean true but it should be boolean false.
Run
  • 54,938
  • 169
  • 450
  • 748
446
votes
9 answers

Is there a Boolean data type in Microsoft SQL Server like there is in MySQL?

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
  • 11,022
  • 17
  • 64
  • 94
433
votes
22 answers

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.
  • 17,596
  • 46
  • 115
  • 165
417
votes
12 answers

Convert boolean to int in Java

What is the most accepted way to convert a boolean to an int in Java?
hpique
  • 119,096
  • 131
  • 338
  • 476
412
votes
13 answers

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
  • 188,729
  • 360
  • 878
  • 1,366
1
2 3
99 100