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
4
votes
6 answers

Python operators returning ints

Is there any way to have Python operators line "==" and ">" return ints instead of bools. I know that I could use the int function (int(1 == 1)) or add 0 ((1 == 1) + 0) but I was wondering if there was an easy way to do it. Like when you want…
None
  • 3,875
  • 7
  • 43
  • 67
4
votes
4 answers

Java boolean[] to byte[] and back

I am sending byte[] arrays over a socket connection in Java. I have a pretty long boolean[] array, where array.length % 8 == 0. I'd like to convert this boolean[] array into a byte[] array with 8 times as few elements, so that I can then send the…
user2714958
4
votes
2 answers

Boolean resets itself to false when getExtra is called

When I invoke getExtras.getBoolean(key) for my isDeleted boolean, it keeps setting itself to false, even though I'm passing in true. Any insight on why this is occurring? I've tried a lot of other methods, but haven't been successful in keeping the…
Kai Tribble
  • 208
  • 1
  • 10
4
votes
4 answers

Boolean vs memory

We had a discussion at work about code design and one of the issues was when handling responses from a call to a boolean method like this: bool ok = IsEverythingOK(); if(ok) { //do somehthing } One of my colleagues insists that we skip the…
Joakim M
  • 1,793
  • 2
  • 14
  • 29
4
votes
3 answers

Should my function return False if it has to return a boolean value?

What are the benefits of writing: def contains(lst, n): for x in lst: if x == n: return True return False instead of: def contains(lst, n): for x in lst: if x == n: return True ? Please note that in the second example the…
Nick
  • 10,309
  • 21
  • 97
  • 201
4
votes
3 answers

How do I cast a bool to a BOOL?

Am I safe in casting a C++ bool to a Windows API BOOL via this construct bool mybool = true; BOOL apiboolean = mybool ? TRUE : FALSE; I'd assume this is a yes because I don't see any obvious problems but I wanted to take a moment to ask only…
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
4
votes
1 answer

Why does '$true -eq "string"' returns $true?

In powerShell you compare a boolean with a string with the "-eq" operator it will always return the same boolean as I used to compare. E.g. $shouldBeFalse = $true -eq "hello" $shouldBeTrue = $false -eq "hello" The variable $shouldBeFalse is…
Patrick
  • 621
  • 2
  • 7
  • 21
4
votes
1 answer

How to create a new df.column based on 2+ conditions in Pandas without iteration?

I have a normal df A = pd.DataFrame([[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]], columns=['A', 'B', 'C'], index=[1, 2, 3, 4, 5]) If I want to create a column based on a condition in another column I do something like…
hernanavella
  • 5,462
  • 8
  • 47
  • 84
4
votes
2 answers

Check if a bool variable is initialized in c#

So I have a bool variable like this in model. bool Foo; I am receiving data from server and de-serialize it model object. So whatever fields are not there in server data gets initialized to defaults. And default(bool) is false. But value false is…
pratpor
  • 1,954
  • 1
  • 27
  • 46
4
votes
1 answer

Why is the Objective-C Boolean data type defined as a signed char?

Something that has piqued my interest is Objective-C's BOOL type definition. Why is it defined as a signed char (which could cause unexpected behaviour if a value greater than 1 byte in length is assigned to it) rather than as an int, as C does…
Andy Bowskill
  • 1,734
  • 3
  • 18
  • 36
4
votes
2 answers

Getting a bool reference from std::vector

I know it's a bad habit, but I'd like to know some workaround or hack for this problem. I have a class like this: template class A : std::vector { T& operator()(int index) { // returns a _reference_ to an object return…
Michal Špondr
  • 1,337
  • 2
  • 21
  • 44
4
votes
5 answers

The precedence of java boolean

&& is supposed to have higher precedence than ||: int a = 10; System.out.println(a==10 || --a==9 && a==8); System.out.println(a); this prints true, and 10. It seems that it checks only the first part and if its true the second never…
xyclops
  • 88
  • 1
  • 5
4
votes
3 answers

Rails 4.x how to query boolean value with activerecord?

I a boolean "guessed" on my model "perks" and I am trying to get all customers that have perks where guessed is true. My first guess was this: @customer = Customer.includes(:perks).where('perks.guessed = true') But that gives me an SQL-exception:…
zkwsk
  • 1,960
  • 4
  • 26
  • 34
4
votes
2 answers

Identifying blocks of TRUEs in list

I'm doing some analysis stuff and using R. I encountered a problem: I have a list of logicals, which looks as follows : list = (TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE) for every logical value a have a list of numeric values : num =…
Jagoda
  • 424
  • 1
  • 5
  • 18
4
votes
2 answers

Why does this static bool not need to be initialized?

if you call checkBool, it will always return "why does this not fail" Why is this and why do you not need to initialize _bool? public sealed class falsefalse { private static bool _bool; public static string checkBool() { if…
puser
  • 477
  • 4
  • 16