Program to Swap Two Numbers Without Using Third Variable Using Arithmetic operators and Bit-wise

Program to Swap Two Numbers Without Using Third Variable Using Arithmetic operators and Bit-wise

codingtutorial

54 года назад

38 Просмотров

#Program_t_ Swap_Two_Numbers_Without Using Third Variable Using Arithmetic operators and Bit-wise

#c_tutorial_2021
#c_programming_2021
#codingtutorial2021
#codingtutorial
#TCS_Technical_interview_Questions_2021
#TCS_Technical_interview _Questions

#infosec_interview_questions_2021
#infosec_interview_questions
all company interview questions


#Program_t_ Swap_Two_Numbers_Without Using Third Variable Using Arithmetic operators and Bit-wise

#c_tutorial_2021
#c_programming_2021
#codingtutorial2021
#codingtutorial
#TCS_Technical_interview_Questions_2021
#TCS_Technical_interview _Questions

#infosec_interview_questions_2021
#infosec_interview_questions
all company interview questions


TCS Technical interview Questions

In the line a = a ^ b, the value 4 is X-ORed with 5 and the result is stored in the variable a. Lets see the X-OR operation of 4 and 5.

The binary format of 4 is 100

The binary format of 5 is 101
-------
001
-------
Answer after performing bitwise X-OR is 1 and that is stored in the variable a.

Next, in the line b = a ^ b, the value 1 is X-ORed with 5 and the result is stored in the variable b. Lets the X-OR operation of 1 and 5.

The binary format of 1 is 001

The binary format of 5 is 101
-------

100

-------
Answer after performing bitwise X-OR is 4 and that is stored in the variable b.

Next, in the line a = a ^ b, the value 1 is X-O Red with 4 and the result is stored in the variable a. Lets the X-OR operation of 1 and 4.

The binary format of 1 is 001

The binary format of 4 is 100
------
101
---
Answer after performing bitwise X-OR is 5 and that is stored in the variable a.
Thus both the values got swapped using X-OR operator.


================================================================

What is Arithmetic overflow?
Method 1 & 2 may cause an arithmetic overflow when the values of a & b are significantly large and here is what we mean by it.

Arithmetic overflow occurs when the result of an arithmetic operation is a value that is too large to fit in the available storage space.

Consider 3 four-bit variables a, b, c. The value stored in the variables a and b is 15 and 1 respectively. We need to add the two variables and store the result in the variable c.
15 in Binary 1111

1 in Binary 0001

---------------------------------

c = 10000

---------------------------------
After adding, the result is 16, we get a 5 bit variable and this is too large to fit in 4-bit variable c. That is how arithmetic overflow occurs.

======================================================================

In this article, we will be discussing the program to swap two numbers without using third variable in C, C++, Java. There are 5 methods to do swap two numbers without using a temporary variable. Below are the methods. #programming_in_c
#C_Programming
#cprogramming
#c_programming_beginners
#programming_for_beginners
#c_programming_for_beginners
#c_programming_tutorial
#programming_c_language
#c_language
#programming_language
#c_programming_language
#c_programming_in_hindi
#c++
#c++_programming
#array
#learn_c_programming
#c_programming course
#saurabh_shukla
#array_c_programming
#array_in_c_programming
#c_program
#programmingwithc
#c_tutorial_for_beginners
#c_programming_tutorial_for_beginners
#function
#c_programming_function
#function_in_c_programming
#do_while_loop_c_programming

Тэги:

#Bit-wise_Operators #c_tutorial #c_programming_2021 #c_language_2021 #codingtutorial #Deepak_Saroj_c_tutorial #deepak_saroj_c_language #deepaksaroj #tcs_interview_2021 #tcs_question_interview_2021 #programming_in_c #C_Programming #c_programming #c_programming_beginners #programming_for_beginners #c_programming_tutorial #programming_c_language #c_language #programming_language #c_programming_language #c_programming_in_hindi #c++ #c++_programming #array #learn_c_programming
Ссылки и html тэги не поддерживаются


Комментарии: