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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:17:19+00:00 2026-06-07T19:17:19+00:00

Here is a program to swap two numbers with out using temporary variable and

  • 0

Here is a program to swap two numbers with out using temporary variable and using shifting operations:

#include <stdio.h>
#include <conio.h>
int main(void)
{
    int a,b,i,j;
    clrscr();
    printf(“Enter two integers: “);
    scanf(“%d%d”,&a,&b);
    printf(“a=%d,b=%d\n”,a,b);
    for(i = 0; i < 16; i++)
    {
        if((a & (1 << i)) ^ (b & (1 << i)))
        {
            a = a ^ (1 << i);
            b = b ^ (1 << i);
        }    
    }
    printf(“a=%d,b=%d”,a,b);
    getch();
    return 0;
}

My question is what is significance of 1 in this program?
I know the method of xoring that works as follows

a = a^b;
b = a^b;
a = a^b;

but I don’t know how above program works?

  • 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-07T19:17:20+00:00Added an answer on June 7, 2026 at 7:17 pm

    1 has one bit on the rightmost position set. 1<<i has one bit on place i set. This program loops through each bit, and swaps them if they are different.

    • a&(1<<i) tests if a has bit i set.
    • ((a&(1<<i))^(b&(1<<i))) tests if bit i in a and b are different.
    • a=a^(1<<i) toggles bit i.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
there is this check-in-out program here at my workplace, it only takes the data
I am trying to send mail using SmtpClient() within my Winforms VB.Net program Here
Here's my program so far: int main() { char choice = 'D'; string inputString;
I have this program here: namespace TodoPlus { using System.Diagnostics; public class LameProg {
I want to swap two xml elements. How do i do this? Here is
I am using an eCard program here to send invitations for an event and
Got a simple Javascript program here to accept and check a password. It should:
I'm trying to build a hello-world program here to check my android kit installation.
Hi i've got here a simple program, but it's not working properly. When i

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.