UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Write a programme to swap two numbers with out using a third variable and by using bitwise op-erators.

Asked by Last Modified  

Follow 0
Answer

Please enter your answer

Computer Science Tutor

a=a+b // a*b // a^b b=a-b // a/b // b^a a=a-b // a/b // a^b
Comments

Take a demo class and you will know

/* Swap a and b */ a ^= b ^= a ^= b ;
Comments

Tuition Classes

main(){ int a=5,b=10; printf("\r\n Before Swap"); printf("\r\na=%d and b=%d",a,b); a^=b^=a^=b; printf("\r\n After Swap"); printf("\r\na=%d and b=%d",a,b); return 0; }
Comments

Technical Corporate Trainer

User XOR ( ^ ) x = x ^ y; y = x ^ y; x = x ^ y;
Comments

SUDHIR SHARMA - Computer Trainer based On Hyderabad

Hi, Ranjeeth.... It's very simple.. just open the following link... http://includehelp.com/c-programs/c-bitwise-programs-to-swap-two-numbers-using-bitwise-operator.aspx
Comments

Tutor

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

Computer Teacher

import java.io.*; class swap{ public static void main(String args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter 1st no: "); int x=Integer.parseInt(br.readLine()); System.out.print("Enter...
read more
import java.io.*; class swap{ public static void main(String args[]) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter 1st no: "); int x=Integer.parseInt(br.readLine()); System.out.print("Enter 2nd no: "); int y=Integer.parseInt(br.readLine()); System.out.println("\nNumbers Before Swapping:"); System.out.println("x = "+x); System.out.println("y = "+y); //Swapping Method 2 x = x^y; y = x^y; x = x^y; System.out.println("\nNumbers After Swapping:"); System.out.println("x = "+x); System.out.println("y = "+y); } } read less
Comments

Expert in ECE skill set.

Int a; Int b; a=a+b; b=a-b; a=a-b; Printf("a=?d b=%d", a,b); ----- take two numbers and check
Comments

Tutor

a = a-b b = a+b a = b-a
Comments

XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).So the code is --------------------------------------- int x = 10, y = 5; x = x ^ y; // x now becomes 15 (1111) y = x ^ y; // y becomes 10 (1010) x = x ^ y; // x becomes 5 (0101)
Comments

View 23 more Answers

Related Questions

What are the control statements in C language?
They are broadly classified into three types: decision-making, iteration, and jump statements. Decision-making statements, like if, else if, and switch, determine the flow of execution based on conditions....
Mukund
0 0
5
I learned C. Shall I learn a new language or keep practicing C?
If you are confident about it then start a new language ,but keep practicing c
Nimesh
0 0
6
Can the last case of a switch statement skip including the break?
Last case if a switch should be DEFAULT.. Good to have this case always. Executed only when there's no other matching cases found.
Sanjeev
What is important topics in C-language for interviews???
Functions, Arrays , String, pointers , structures
Gayu

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Is It Fine To Write “void main()” Or “main()” In C/C++?
The definition: void main() { /* ... */ } Is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1 or the ISO C standard 5.1.2.2.1. A conforming...

Swapping variable contents using C
/* WAP to swap the content of variables using C*/ //Header files #include<stdio.h>#include<conio.h> //Main function void main(){ //Variable declaration of type integer int a,b,c; //function...

Memory Layout of C Programs
A typical memory representation of C program consists of following sections. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object...

Structures in C
A structure is a collection of one or more data members possibly of different data types, grouped together under a single name for convenient handling. Defining a Structure: In general terms,...

Operators in C
Operators in C Operator: An operator is a symbol that tells the compiler to perform certain mathematical or logical calculations. Operators are used in programs to manipulate data and variables. The...

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more