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 8268251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:48:59+00:00 2026-06-08T05:48:59+00:00

How to change the sign of int using bitwise operators? Obviously we can use

  • 0

How to change the sign of int using bitwise operators? Obviously we can use x*=-1 or x/=-1. Is there any fastest way of doing this?

I did a small test as below. Just for curiosity…

public class ChangeSign {
    public static void main(String[] args) {
        int x = 198347;
        int LOOP = 1000000;
        int y;
        long start = System.nanoTime();
        for (int i = 0; i < LOOP; i++) {
            y = (~x) + 1;
        }
        long mid1 = System.nanoTime();
        for (int i = 0; i < LOOP; i++) {
            y = -x;
        }
        long mid2 = System.nanoTime();
        for (int i = 0; i < LOOP; i++) {
            y = x * -1;
        }
        long mid3 = System.nanoTime();
        for (int i = 0; i < LOOP; i++) {
            y = x / -1;
        }
        long end = System.nanoTime();
        System.out.println(mid1 - start);
        System.out.println(mid2 - mid1);
        System.out.println(mid3 - mid2);
        System.out.println(end - mid3);
    }
}

Output is almost similar to :

2200211
835772
1255797
4651923
  • 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-06-08T05:49:01+00:00Added an answer on June 8, 2026 at 5:49 am

    The speed difference between non-floating point (e.g. int math) addition/multiplication and bitwise operations is less than negligible on almost all machines.

    There is no general way to turn an n-bit signed integer into its negative equivalent using only bitwise operations, as the negation operation looks like x = (~x) + 1, which requires one addition. However, assuming the signed integer is 32 bit you can probably write a bitwise equation to do this calculation. Note: do not do this.

    The most common, readable way to negate a number is x = -x.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

change() function works and detects changes on form elements, but is there a way
How can I change the sign of a number? abs(); will not return me
Is there a way to change the name of the auto-generated Inno uninstaller and
why the md5 checksum always change when i sign the file? I have the
I have scroll on a div. And I want to change my cursor sign,
I have a string.Format like this : string Test = string.Format({0:#,0}, NegativeNumber); how can
I have this html: <div id=signbtn> <a id=login-link class=btnsignin title=Login>Sign in</a> </div> Upon successful
I would like to know how to change the colour of the equality sign
Am using the Devise plugin for user sign-in. I want to to display an
I'm aware that you can change the names of routes for devise, but I'm

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.