Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: boolean to int : was char to decimal Date: Fri, 06 May 2011 07:07:14 -0700 Organization: A noiseless patient Spider Lines: 56 Message-ID: References: <92ea64F3avU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 6 May 2011 14:07:26 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="l8hxu7VxWTF0R70MQnZDgg"; logging-data="19238"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pyAyktrZx25xhsmPwR6KWLgZi/WcuF3g=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:cja4TNmcS9zV3sgEPTUlnDysZ1g= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3686 On 5/6/2011 6:00 AM, Jeff Higgins wrote: > C++ test: > int test = (int(d2 > e) << 1) + int(d3 > e); > > Java test: > int t = d2 > e ? 1<<1 : 0; ^^^^ Well, this is a 2. The Java coding conventions say to declare this as a constant, I'm not sure that is valuable here though > int test = d3 > e ? t+1 : t; t += d3 > e ? 1 : 0; The whole thing could be done in one go, of course: int test = ((d2 > e) ? 2 : 0) + ((d3 > e) ? 1 : 0); > > switch(test) > { case(0): > case(1): > case(2): > case(3): > } Blech. So the whole point here seems to be to set the 1 bit if d2 > e, and set the 0 bit if d2 > e. Why not just do that? if( d2 > e ) { if( d3 > e ) { // case 0 } else { // case 1 } } else { if( d3 > e ) { // case 2 } else { // case 3 } } I don't think there's more tests for the CPU in this case. In fact the jump to implement the switch might be more expensive that just a simple branch test. I'd have to know what is motivating the use of switch instead of if-else. Overall, I feel the last one, the if-else in Java, is the most clear and maintainable. Given your apparent confusing trying to translate the C++ version literally, I think I'd say that promoting booleans to ints is a bad idea, as it produces crap.