Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #3279

Re: Java left shift and right shift operators.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: Java left shift and right shift operators.
Date Tue, 26 Apr 2011 14:55:14 -0400
Organization albasani.net
Lines 92
Message-ID <ip74ec$u1j$1@news.albasani.net> (permalink)
References <295e16b3-2ed8-4529-bfb0-1cc26ed93ad6@d26g2000prn.googlegroups.com> <1b558330-ae94-4e4e-9922-a9aeb63eaf37@d19g2000prh.googlegroups.com> <ip6ak8$6ie$1@dont-email.me> <44bbc323-c260-4169-ab2a-11b95d9e73e8@34g2000pru.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net +TckZnG4F7HXxpzXI8SYp4a4PshI/7QpxoXtBY2h3SAJi0s3ABIwMhtVj/GA6nbDW4hTs/VmxNJhk+OS0MYUCtml5zpW++rtd6p3/nuhiZYONEj4ulrHV607QTDglwAt
NNTP-Posting-Date Tue, 26 Apr 2011 18:55:09 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="wzc/dtGTp+mKOTxdJTBYSUmjZFq5ZxAi9lNt6d6Y9DGq/TRokVCXvIVPtlP4u+Ov7BHkVtquNpPRbKdnIbeQmmw/UW8OJnPLdCoVakNIhgVilmuTj3fDXdSdvztWH0D4"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8
In-Reply-To <44bbc323-c260-4169-ab2a-11b95d9e73e8@34g2000pru.googlegroups.com>
Cancel-Lock sha1:HN9D1t7OM7G+Lt5gJ7ACbcUTBR8=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3279

Show key headers only | View raw


Sanny, please attribute your citations.

Sanny wrote:
>>>> Is there any way to avoid the if condition and do right shift and left
>>>> shift using operators depending on shiftby is +ve or -ve?
>>
>>> I used below technique but it gives error.
>>
>>> divider=Math.pow(2,shiftby);
>>> then
>>> output = (long) N / divider;

Eric Sosman wrote:
>>       Let me get this straight: You're worried about the inefficiency
>> of an `if' that chooses between a left or a right shift, so you plan
>> to eliminate the `if' by evaluating an exponential function?
>>
>>       ARE YOU OUT OF YOUR MIND?

Sanny wrote:
> I didnt knew how much inefficient it is. I heard Math. (...) functions
> are very fast and they use Native support like assembly language so
> they are quite faster.

Whence did you hear that?  Please let us know the source(s).

Think through on the basis of fundamental computer science.  Math.xxx() 
methods on floating point ('float' and 'double') are always going to be slower 
than integer operations, particularly single-instruction operations like 
shift.  However, the difference isn't always going to matter.

So when some anonymous, untrusted source tells you that a Math.xxx() method is 
"very fast", the context OBVIOUSLY is relative to other implementations of the 
same functionality, not "'Math.pow()' is faster than '>>>'".  If you saw 
*that* claim, throw away the source immediately.

Programming is an art that requires the practitioner to *think*.  Cargo-cult 
reactions based on no evidence or reason or even thought, such as "Math.pow() 
is 'very fast', so I'll use it instead of shift operators without measuring 
first what impact if any the shift operators have on actual performance."

> Is there a "Integer" in java [psic] which is longer than 64 bits?

RTFM. [1]

> byte: 8 bits
> short: ???
> int: 16/32 bits

Where do you get this?  Type sizes in Java are fixed to particular values, not 
"16/32".

> long: 64 bit
>
> any 128 bit integer???

How many question marks does it take to indicate an interrogative?

> I want something that can handle more bits than long. and I am able to
> do bit operations on them.
>
> I have heard of BigInteger I dont know how many bits it supports.

RTFM. [2]

> Can I use byte[] to represent a longer number and do multiplications/
> divisions on them?

Yes.

But why would you?

As for RTFMing, you will make zero progress as a programmer, in fact, you are 
not even a programmer unless you habitually and competently read and 
comprehend the documentation.

One who does not habitually and competently read and comprehend the 
documentation will learn less than nothing from questions asked and answers 
received of Usenet.

Assimilation of that advice will accelerate your skills a thousandfold.

[1]
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.1>

[2]
<http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html>

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Java left shift and right shift operators. Sanny <softtanks22@hotmail.com> - 2011-04-26 00:38 -0700
  Re: Java left shift and right shift operators. Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-04-26 09:55 +0200
    Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-26 03:16 -0700
    Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 09:49 -0400
      Re: Java left shift and right shift operators. Travers Naran <tnaran@gmail.com> - 2011-04-26 07:09 -0700
        Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 11:21 -0400
        Re: Java left shift and right shift operators. Martin Gregorie <martin@address-in-sig.invalid> - 2011-04-26 15:37 +0000
      Re: Java left shift and right shift operators. Sanny <softtanks22@hotmail.com> - 2011-04-26 09:32 -0700
        Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-26 11:09 -0700
          Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-26 13:09 -0700
        Re: Java left shift and right shift operators. Travers Naran <tnaran@gmail.com> - 2011-04-26 22:40 -0700
          Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-27 05:34 -0700
            Re: Java left shift and right shift operators. Owen Jacobson <angrybaldguy@gmail.com> - 2011-04-28 00:37 -0400
              Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-28 16:02 -0700
              Re: Java left shift and right shift operators. Michael Wojcik <mwojcik@newsguy.com> - 2011-04-28 19:22 -0400
          Re: Java left shift and right shift operators. bugbear <bugbear@trim_papermule.co.uk_trim> - 2011-04-27 14:28 +0100
          Re: Java left shift and right shift operators. Thomas Richter <thor@math.tu-berlin.de> - 2011-04-30 00:02 +0200
      Re: Java left shift and right shift operators. Roedy Green <see_website@mindprod.com.invalid> - 2011-04-27 19:25 -0700
        Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-27 23:08 -0400
  Re: Java left shift and right shift operators. Sanny <softtanks22@hotmail.com> - 2011-04-26 02:04 -0700
    Re: Java left shift and right shift operators. Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-26 07:34 -0400
      Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 09:47 -0400
        Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-27 09:23 -0700
          Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-27 14:49 -0400
            Re: Java left shift and right shift operators. Paul Cager <paul.cager@googlemail.com> - 2011-04-27 17:28 -0700
              Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-27 22:06 -0400
      Re: Java left shift and right shift operators. Sanny <softtanks22@hotmail.com> - 2011-04-26 09:39 -0700
        Re: Java left shift and right shift operators. Patricia Shanahan <pats@acm.org> - 2011-04-26 11:17 -0700
        Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 14:55 -0400
          Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 14:57 -0400
          Re: Java left shift and right shift operators. Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-26 21:38 +0200
          Re: Java left shift and right shift operators. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-26 23:32 +0000
            Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-27 00:08 -0400
              Re: Java left shift and right shift operators. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-27 06:57 +0000
                Re: Java left shift and right shift operators. Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-27 07:00 -0300
                Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-27 08:42 -0400
                Re: Java left shift and right shift operators. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-27 14:03 +0000
                Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-27 14:54 -0400
        Re: Java left shift and right shift operators. Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-26 21:14 -0400
    Re: Java left shift and right shift operators. Travers Naran <tnaran@gmail.com> - 2011-04-26 07:30 -0700
      Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 11:24 -0400
        Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 11:35 -0400
          Re: Java left shift and right shift operators. Sanny <softtanks22@hotmail.com> - 2011-04-26 09:46 -0700
            Re: Java left shift and right shift operators. Lew <noone@lewscanon.com> - 2011-04-26 15:02 -0400
      Re: Java left shift and right shift operators. Lothar Kimmeringer <news200709@kimmeringer.de> - 2011-04-28 09:14 +0200
  Re: Java left shift and right shift operators. Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-04-26 08:41 -0400
  Re: Java left shift and right shift operators. Robert Klemme <shortcutter@googlemail.com> - 2011-04-26 22:06 +0200

csiph-web