Java’s Broken Booleans

  • Thread starter Lawrence D'Oliveiro
  • Start date
L

Lawrence D'Oliveiro

It is well-known that xor'ing a bit with 1 is equivalent to flipping it,
i.e. a = not a. Since 0 is false, if you xor each bit with 1, you get
not 0 for every bit, so 0 xor "all 1s" = 0 xor -1 = -1.

That’s no excuse.
 
L

Lawrence D'Oliveiro

bugbear said:
At least you've got that right.

So do you understand the difference between a circular buffer and double-
buffering? A circular buffer can be any size, but double-buffering doesn’t
work any better if you add a third buffer. Do you get that much?
 
L

Lawrence D'Oliveiro

Daniele Futtorovic said:
Yeah, you're right, these things suck. Tell you what -- how about we
created a new type with only two possible values to represent booleans?

What a wonderful idea!

The only thing I can't decide is: should we cripple it, or should we make it
useful? That’s a hard one...
 
L

Lawrence D'Oliveiro

|| is a short-circuit operator.

That’s OK, it makes no difference in the situations where I would write “A
<= Bâ€.
As I said, I care more about readability of code than speed of
execution. If you think that is just plain stupid, then I would not want
to hire you for a project management team.

Let me just point out that verbosity is no substitute for clarity. You don’t
make your code easier to understand simply by writing more of it.
 
J

Joshua Cranmer

So do you understand the difference between a circular buffer and double-
buffering? A circular buffer can be any size, but double-buffering doesn’t
work any better if you add a third buffer. Do you get that much?

Obviously, you've never done high-performance graphics work. With more
buffers, you can make a deeper graphics pipeline.
 
J

Joshua Cranmer

That’s no excuse.

It's a hypothesis for why that design decision is made.

What makes {0, 1} superior to {0, -1}? Or {-1, +1}? Or {-0, +0}, for
that matter? And saying "that's what (insert favorite language here)
does" is not a valid answer.
 
L

Lew

It's a hypothesis for why that design decision is made.

What makes {0, 1} superior to {0, -1}? Or {-1, +1}? Or {-0, +0}, for that
matter? And saying "that's what (insert favorite language here) does" is not a
valid answer.

It's the one most likely to engender disagreement, thus feeding the troll.
 
L

Lawrence D'Oliveiro

Peter is saying that Java supports the functionality that you wish
while I am saying that you are using the wrong syntax for it.

So your point is that it is perfectly acceptable not to be able to use a
built-in comparison operator to perform a perfectly meaningful comparison on
a built-in type?
 
J

Joshua Cranmer

The fact that it is more useful. I gave examples before.

Let's see if I get all of them:
1. Indexing into an array. Okay, all of the formats I gave can do this
quite nicely with at most a simple arithmetic expression, especially in
languages that don't limit you to a 0-n-1 format for an array. I should
also point out that Basic uses 1-based array indexing, so it doesn't
work without such an expression anyways.

2. Boolean comparisons. Okay, flip the sign. Nothing changes, except
that now the implicit arrow actually goes in a direction that actually
makes it visually identical to the implication arrow. Note that nothing
inherent in true versus false as elements of a group of order 2 implies
that one should be greater than the other.

I might also add that nothing inherently implies that 0 should be false
as opposed to true, although you might argue that the group of {true,
false} with the xor operation yields false as the identity element, and
0 is the identity for integers under addition... but under the xnor
operation, true is the identity, and 1 is the identity for integers
under multiplication, so you can clearly argue for other homomorphisms.

In short: I see nothing in your arguments which necessarily implies that
{0, 1} is greater than any other mapping of {true, false} into the integers.
 
L

Lawrence D'Oliveiro

The fact that it is more useful. I gave examples before.

Let's see if I get all of them:
1. Indexing into an array. Okay, all of the [extra work] I gave can do
this quite nicely ...

2. Boolean comparisons. Okay, [extra work]. Nothing changes, except ...

I might also add that nothing inherently implies that 0 should be false
as opposed to true ...

Except that’s how Boolean algebra normally writes it, together with “+†for
“or†and concatenation (as per multiplication) for “andâ€. That’s how George
Boole himself wrote it, complete with “1 - x†to denote “not xâ€.
In short: I see nothing in your arguments which necessarily implies that
{0, 1} is greater than any other mapping of {true, false} into the
{integers.

Except for all the extra work you have to do.
 
L

Lew

bugbear said:
Lawrence's experience and knowledge seems ... shallow.

We cannot know what experience and knowledge he may have ... scratch that, we
know he's an idiot.
 
J

Joshua Cranmer

Except for all the extra work you have to do.

Extra work? The mapping {0, -1} turns boolean not into a xor -1, and or
and and stay the same. The mapping {0, 1} turns boolean not into 1 - a,
which could possibly be slower on some machines (IIRC, some machines can
do the bitwise operands in 1 clock cycle and addition/subtraction in 2
clock cycles).
 
A

Andreas Leitgeb

Extra work? The mapping {0, -1} turns boolean not into a xor -1, and or
and and stay the same. The mapping {0, 1} turns boolean not into 1 - a,

.... or, equivalently in the domain of interest: xor 1
which could possibly be slower on some machines

Otoh, a conditional jump for boolean-to-int mapping (be
it with "if" or "?:"), as in Java, is (not just "possibly",
but even "most likely") slower on modern hardware, even
than obtaining the logarithm of a float/double...
 
L

Lawrence D'Oliveiro

Obviously, you've never done high-performance graphics work. With more
buffers, you can make a deeper graphics pipeline.

And I could say, obviously, you’ve never done asynchronous, interrupt-driven
I/O work.

Horses for courses.
 
D

Daniele Futtorovic

What a wonderful idea!

The only thing I can't decide is: should we cripple it, or should we make it
useful? That’s a hard one...

Funny, I was just thinking the same thing about you.

Trouble is, I don't care enough for the former, and the latter seems too
far fetched.

--o
 
L

Lawrence D'Oliveiro

Here's one: COBOL. Level-88 items let you define the representation of
"true" and "false" for a given variable, so they can have any ordering
you like.

That’s like saying that, just because the right-hand rule can be replaced
with a left-hand rule without introducing any inconsistencies, that there
shouldn’t be a right-hand rule.
 
M

Martin Gregorie

That’s like saying that, just because the right-hand rule can be
replaced with a left-hand rule without introducing any inconsistencies,
that there shouldn’t be a right-hand rule.
No, its a way of providing meaningful names for values and hence make the
program more readable:

01 ACCOUNT-STATUS PIC 9.
88 NOT-YET-ACTIVATED VALUE 0;
88 ACTIVE-ACCOUNT VALUE 1.
88 SUSPENDED-ACCOUNT VALUE 2.

So you can write

IF ACTIVE-ACCOUNT
PERFORM CALCULATE-CREDIT-RATING.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top