Re: Negation of boolean

From: Roedy Green (look-on_at_mindprod.com.invalid)
Date: 07/30/04


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.


Relevant Pages

  • Re: Do I have to close all streams from Process Class after using the Process object?
    ... I have to use Process proc = Runtime.getRuntime.execmethod to ... When I traced Java source code, ... My question is do I have to close all these streams ... runtime, final boolean strongRef, final boolean weakRef, final ...
    (comp.lang.java.programmer)
  • Re: About Javas Collection
    ... WHY does Java was designed like this, ... boolean contains ... reasonable to ask if an object not proven to be of type E is a member. ... The deeper answer to your "WHY" is that generics were not ...
    (comp.lang.java.programmer)
  • Re: Interupting the current thread
    ... public class StoppableThread extends Thread { ... * @param interrupt ... public void gentleStop (boolean interrupt, ... http://mindprod.com Java custom programming, consulting and coaching. ...
    (comp.lang.java.help)
  • Re: Rubys lack of variable declarations : Good or Bad?
    ... I recently wrote a small package in Java that parses and fetches ... If you are changing lFlag from a boolean to an integer, ... A static language will make it easer ... What interface does that parameter have to support? ...
    (comp.object)
  • Re: Groovy (war Re: [PROST]Re: CDC Plugin fuer Eclipse)
    ... >>In Java gibt es das nicht, also kann Java sich kein if definieren ohne ... >>ein if, switch, den trinären Operator oder eine Schleife zu benutzen. ... > class True extends Boolean_ ... Previous by thread: ...
    (de.comp.lang.java)

Loading