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 11:35:38 -0400 Organization: albasani.net Lines: 22 Message-ID: References: <295e16b3-2ed8-4529-bfb0-1cc26ed93ad6@d26g2000prn.googlegroups.com> <1b558330-ae94-4e4e-9922-a9aeb63eaf37@d19g2000prh.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net EybgaeBw17HT3Uh+GPDMcwLNQcVtpSpDUyvzivzkGUScte2V7h2m5eaftLsvLl/ef0LPjDhrhfK2pnItvnkjRg== NNTP-Posting-Date: Tue, 26 Apr 2011 15:35:33 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="NJ3DsLO45viVrQHfRHBbHaMtjA35dHbRp81fvc7w19MXTOAAbJBNrIhzQNgB9xv0jGCg2rYIr+i4IbtulQTl7UYda0fsI86IQCmfnHmizocu1AXm4Bs6tVu+t1XiD+X0"; 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: Cancel-Lock: sha1:4rKAmQ3QLBgSJhpaQRaPyYCyiBM= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3269 Lew wrote: > Doesn't matter, the Java operator *is* defined for negative values. > > So the solution to Sanny's problem is: > > output = n >> shiftBy; OK, that is a trick answer because it's wrong. True, the shift operator is defined for negative shift values, but those get masked to positive shift values regardless and don't give the OP's desired result. Consider shifting an int by either 12 or -12 12 = 0b00001100 -12 = 0b11110100 The bottom five bits of the latter evaluate to 20. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg