Re: Negation of boolean
From: Roedy Green (look-on_at_mindprod.com.invalid)
Date: 07/30/04
- Next message: diffused: "searching elements of an array within another array"
- Previous message: thufir.hawat_at_mail.com: "Re: Negation of boolean"
- In reply to: thufir.hawat_at_mail.com: "Re: Negation of boolean"
- Next in thread: Chris Smith: "Re: Negation of boolean"
- Reply: Chris Smith: "Re: Negation of boolean"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Jul 2004 04:45:38 GMT
On Fri, 30 Jul 2004 04:13:05 GMT, thufir.hawat@mail.com wrote or
quoted :
>I always thought "true==1" anything else is false. Anyhow, what would be
>the correct true reprensentation, and conversely, false?
Java has the values true and false at the source code level. There is
officially no numeric equivalence. However, when you do a
DataOutputStream of a boolean you get the numbers 1 and 0, so at least
externally Java is constrained to represent true as 1 and false as 0.
Similarly for JNI and serialised objects.
In Forth 0 is false and non-zero is true, with a canonical true
represented as -1. This representation is the most efficient from the
point of view of bit and logical manipulations in assembler. You can
do simple booleans or word sized chunks of many bits at once with the
same operators.
A clever optimiser could use the Forth representation for a purely
local boolean. Then for example it could implement ! simply using the
Pentium NOT instruction, or by storing a boolean variable with inverse
logical sense if that turned out to be more convenient. A optimiser
can use deMorgan's laws to simplify your expressions.
Ironically, | is sometimes faster than || since you avoid the jump. A
smart optimiser would convert || to | for you when it would be faster
and the second computation being done would be harmless.
Java and C programmers tend to think of boolean assignment as less
than first class. They prefer to think with ifs and conditions.
Boolean assignment seems unnatural.
You will rarely see them using code like this:
boolean deletable = s.endsWith(".tmp") || s.startWith( "temp" );
You will see instead the boolean expression repeated, or the boolean
computed like this:
boolean deletable = false;
if ( s.endsWith(".tmp") ) deletable = true;
else if ( s.startsWith("temp") ) deletable = true;
Ever since my FORTRAN days, I have been trying to encourage my fellow
programmers to use boolean assignment more often. It usually generates
more readable code in my opinion.
It also lets you build up very complicated expressions from simpler
pieces.
-- Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
- Next message: diffused: "searching elements of an array within another array"
- Previous message: thufir.hawat_at_mail.com: "Re: Negation of boolean"
- In reply to: thufir.hawat_at_mail.com: "Re: Negation of boolean"
- Next in thread: Chris Smith: "Re: Negation of boolean"
- Reply: Chris Smith: "Re: Negation of boolean"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|