Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew 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: References: <295e16b3-2ed8-4529-bfb0-1cc26ed93ad6@d26g2000prn.googlegroups.com> <1b558330-ae94-4e4e-9922-a9aeb63eaf37@d19g2000prh.googlegroups.com> <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 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] [2] -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg