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

  • Home
  • SEARCH
  • 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 618255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:31:29+00:00 2026-05-13T18:31:29+00:00

I’ve recently been writing some code for a research project that I’m working on,

  • 0

I’ve recently been writing some code for a research project that I’m working on, where efficiency is very important. I’ve been considering scraping some of the regular methods I do things in and using bitwise XORs instead. What I’m wondering is if this will make if a difference (if I’m performing this operation say several million times) or if it’s the same after I use 03 in g++.

The two examples that come to mind:

I had an instance where (I’m working with purely positive ints) I needed to change n to n-1 if n was odd or n to (n+1) if n was even. I figured I had a few options:

if(n%2) // or (n%2==0) and flip the order
    n=n-1
else
    n=n+1

or

n=n+2*n%2-1; //This of course was silly, but was the only non-bitwise 1 line I could come up with

Finally:

n=n^1;

All of the methods clearly do the same thing, but my feeling was that the third one would be the most efficient.

The next example is on a more general note. Say I’m comparing two positive integers, will one of these perform better than the others. Or will the difference really not be noticeable, even if I perform this operation several million times:

if(n_1==n_2)
if(! (n_1 ^ n_2) )
if( n_1 ^ n_2) else \do work here

Will the compiler just do the same operation in all of these instances? I’m just curious if there is an instance when I should use bitwise operations and not trust the compiler to do the work for me.

Fixed:In correct statement of problem.

  • 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-13T18:31:30+00:00Added an answer on May 13, 2026 at 6:31 pm

    It’s easy enough to check, just fire up your disassembler. Take a look:

    f.c:

    unsigned int f1(unsigned int n)
    {
      n ^= 1;  
      return n;
    }
    
    unsigned int f2(unsigned int n)
    {
      if (n % 2)
        n=n-1;
      else
        n=n+1;
    
      return n;
    }
    

    Build and disassemble:

    $ cc -O3 -c f.c 
    $ otool -tV f.o 
    f.o:
    (__TEXT,__text) section
    _f1:
    00  pushq   %rbp
    01  movq    %rsp,%rbp
    04  xorl    $0x01,%edi
    07  movl    %edi,%eax
    09  leave
    0a  ret
    0b  nopl    _f1(%rax,%rax)
    _f2:
    10  pushq   %rbp
    11  movq    %rsp,%rbp
    14  leal    0xff(%rdi),%eax
    17  leal    0x01(%rdi),%edx
    1a  andl    $0x01,%edi
    1d  cmovel  %edx,%eax
    20  leave
    21  ret
    

    It looks like f1() is a bit shorter, whether or not that matters in reality is up to some benchmarking.

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

Sidebar

Ask A Question

Stats

  • Questions 482k
  • Answers 482k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I use a "Logging" Action Filter to log all calls… May 16, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer The timestamp is updated when the data that represents the… May 16, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer It's because of integer division; 1/2 = 0, 0 *… May 16, 2026 at 6:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.