Re: Negation of boolean
From: Adam (notreal_at_me.com)
Date: 07/30/04
- Next message: Carl Smotricz: "Re: Question on CVS..."
- Previous message: Carl Smotricz: "Re: newbie lost in strings......"
- In reply to: Roedy Green: "Re: Negation of boolean"
- Next in thread: Roedy Green: "Re: Negation of boolean"
- Reply: Roedy Green: "Re: Negation of boolean"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Carl Smotricz: "Re: Question on CVS..."
- Previous message: Carl Smotricz: "Re: newbie lost in strings......"
- In reply to: Roedy Green: "Re: Negation of boolean"
- Next in thread: Roedy Green: "Re: Negation of boolean"
- Reply: Roedy Green: "Re: Negation of boolean"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|