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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:46:48+00:00 2026-06-06T00:46:48+00:00

Why the two printf statements are outputting different values? int main() { int n=10;

  • 0

Why the two printf statements are outputting different values?

int main()
{
    int n=10;

    printf("%d\n",(n&0xAAAAAAAA)>>1 + n&0x55555555  ); //prints 0

    printf("%d\n", n&0x55555555 + (n&0xAAAAAAAA)>>1 ); //prints 10

    return 0;
}

http://ideone.com/B33YB

  • 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-06T00:46:50+00:00Added an answer on June 6, 2026 at 12:46 am

    Because of the operator precedence.

    + is executed earlier than >>.

    When you change

    (n&0xAAAAAAAA)>>1 + n&0x55555555)

    to

    n&0x55555555 + (n&0xAAAAAAAA)>>1)

    you are actually changing the order in which the operations are executed.

    (n&0xAAAAAAAA)>>1 + n&0x55555555 can be rewritten as (n&0xAAAAAAAA)>>(1 + n&0x55555555) which is different compared to ((n&0xAAAAAAAA)>>1) + n&0x55555555 (which is what the second line states)

    The same goes for the + and the & operator.

    So to make the output their outputs similar you need additional parenthesis:

    int main()
    {
        int n=10;
    
        printf("%d\n",((n&0xAAAAAAAA)>>1) + (n&0x55555555) ); // prints 5
        printf("%d\n",(n&0x55555555) + ((n&0xAAAAAAAA)>>1) ); // prints 5
    
        return 0;
    }
    

    See http://ideone.com/d3mHT

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

Sidebar

Related Questions

I have these two statements : printf(%u,a+1); and printf(%u,(int *)a+1); Actually I was working
What is the difference between the two statements below. They are rendering different outcomes,
please take a look at the two following c statements printf(a very long string);
Here is one program #include<stdio.h> #include<stdlib.h> int main() { unsigned char a=0x80; printf(%d\n,a<<1); }
proof of point http://adams-site.x10.mx/v/python.png You'll notice in this image the two print statements are
When I use printf/printf_s with two strings, I get the same output for both
In C, what is the difference between these two? float myF = 5.6; printf(
I want these two print functions to do the same thing: unsigned int Arraye[]
I noticed recently that there are two ways to print multiple statements in PHP.
I'm currently using the following two command to generate client statements populated from mysql

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.