Re: Negation of boolean

From: Adam (notreal_at_me.com)
Date: 07/30/04


Date: Fri, 30 Jul 2004 01:14:55 +0100


----- Original Message -----
From: "Roedy Green" <look-on@mindprod.com.invalid>
Newsgroups: comp.lang.java.help
Sent: Thursday, July 29, 2004 9:23 PM
Subject: Re: Negation of boolean

>
> Here are my discoveries:
>
> ^= is the tersest source code.
>
> ^= is the code most likely to baffle an average programmer.
>
> ^= generates the shortest JVM byte code.
>
> ! generates the fastest code in the case where the expression is false
> by a considerable margin.
>
> ^= generates slightly faster code in the case where the expression is
> true.
>
> I don't know how to snapshot what machine language code finally gets
> generated in Hotspot.
>
> Here are my opinions.
>
> ^= The tersest code is usually the most readable by a knowledgeable
> programmer. There are fewer symbols to absorb. So long as you are
> dealing with idiomatic patterns, such as x ^= condition, there is no
> problem grasping what that does in a gestalt.
>
> ^= is an operator in the Java language. If you don't know what a basic
> operator does, you can't consider yourself a profession Java
> programmer. Everyone who writes Java code for a living should know off
> the bat what this means, or be capable of figuring it out is less than
> a minute.
>
> ^ has fascinating properties. People should want to learn about them
> just out of wonder.
>
> ^ should be more widely used than it is.
>
> It is not wise to use ^ in a team project where you are likely to have
> anything but first class programmers participating. They will play
> helpless. They have no way of figuring it out since somebody else did
> their homework for them when they were going through school. If you
> do use it, you need commentary explaining what it does. Further, there
> exists a quite serviceable more standard alternative.
>

It seems that the java compiler creates a conditional branch when negating a
boolean using !b, which is 3to4 steps, always including one branch:
to negate a boolean at the top of the stack in jvm instructions
0:iload_0
1:ifne 8
4:iconst_1
5:goto 9
8:iconst_0
9....

whereas b^true is just a load, load const1 and xor. which is always just 3
steps, with no branching

Using xor to negate a boolean seems to be a faster (40% on my machine) than
negating using !, is that always the case and if so, why doesn't the
compiler take advantage of this and always compile !b to b^true?

adam



Relevant Pages

  • Re: Regarding EVALUATE TRUE
    ... where I built the Boolean truth table and simplification. ... understanding the outer EVALUATE first. ... Will Mr. Dashwood please step forward to apply his De Morgan's ... programmer "can be too clever for his own good", or more accurately, a ...
    (comp.lang.cobol)
  • 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)