Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:35:17+00:00 2026-05-23T14:35:17+00:00

I want to implement f(int x) { return x == 0 ? 0 :

  • 0

I want to implement f(int x) { return x == 0 ? 0 : 1; } in Java.

In C, I’d just “return !!x;“, but ! doesn’t work like that in Java. Is there some way to do it without conditionals? Without something cheesy like an unrolled version of

int ret = 0;
for (int i = 0; i < 32; i++) {
    ret |= ((x & (1 << i)) >>> i);
}

or

try {
   return x/x;
} catch (ArithmeticException e) {
   return 0;
}

)

EDIT:

So, I did a microbenchmark of three different solutions:

  1. my return x/x catch solution,
  2. the obvious x==0?0:1 solution, and
  3. Ed Staub’s solution: (x|-x) >>> 31.

The timings for random int inputs (the whole int range) were:

1. 0.268716  
2. 0.324449  
3. 0.347852  

Yes, my stupid x/x solution was faster by a pretty hefty margin. Not very surprising when you consider that there are very few 0’s in it, and in the vast majority of cases the fast path is taken.

The timings for the more interesting case where 50% of inputs are 0:

1. 1.256533  
2. 0.321485  
3. 0.348999  

The naive x==0?0:1 solution was faster by about 5% than the clever one (on my machine). I’ll try to do some disassembly tomorrow to find out why.

EDIT2:
Ok, so the disassembly for the conditional version is (excluding book-keeping):

testl rsi,rsi
setnz rax
movzbl rax,rax

The disassembly for (x|-x)>>>31 is:

movl rax,rsi
negl rax
orl rax,rsi
sarl rax,#31

I don’t think anything else needs to be said.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:35:18+00:00Added an answer on May 23, 2026 at 2:35 pm

    Ok, shortest solution without conditional is probably:

    return (i|-i) >>> 31;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to implement an interface that combines some instances of
I'm trying to work with fractions in Java. I want to implement arithmetic functions.
I have some old code (pre Java 1.5) that uses classes to implement type
Already finished implementing the player. I want to implement the progress bar. But I
I am trying to implement a simple HashTable in Java that uses a Linked
I'm writing a Java game and I want to implement a power meter for
My question isn't specific to Java but that is the language I'm using to
Is there something like anonymous inner classes (used in Java) in C#? I explain
I want to implement the union, intersect, difference and reverse operations in Java. First
I have a factory class that populates objects with data. I want to implementing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.