Number 4 100 Bitwise XOR 1 001 Java Program to Swap Two Number Using Bitwise XOR operator Java class Swap { public static void main(String[] args) { int a = 11, b = 22; System.out.println("Before swapping the numbers:"); System.out.println("First number = " + a); System.out...
// Rust program to swap two numbers// using bitwise XOR (^) operatorfnmain() {letmutnum1:i32=6;letmutnum2:i32=2; println!("Numbers before swapping:"); println!("\tNum1: {}",num1); println!("\tNum2: {}",num2); num1=num1^num2; num2=num1^num2; num1=num1^num2; pri...
import Swift; Here, we created two integer variablesnum1andnum2that are initialized with 5, 8 respectively. Then we interchanged the values of variables using the bitwise XOR (^) operator and printed the result on the console screen.
XOR the two numbers again and store the result in the first number (x = 2 ^ 5 so x = 7)Below is the program to swap two numbers using bitwise operator.#include<stdio.h> #include<conio.h> void main() { int x = 6, y = 4; x = x^y; y = x^y; x = x^y; printf("x ...
First, you can swap integers only. Secondly, be aware of the numbers overflow when performing the addition at the first stepa = a + b(the sum must be lower thanNumber.MAX_SAFE_INTEGER). 4. Bitwise XOR operator The XOR operator evaluates to true if the operands are different. As a rem...
Approach 3: Using XOR Bitwise Operator You can also use an advanced approach to swapping two numbers is by using the XOR bitwise operator. Example The following example demonstrates swapping two integers using the XOR bitwise operator: packagemainimport"fmt"funcmain(){varnum1int=10varnum2int=20fm...
This program will swap two bytes/words of an integer number, here this operation is implemented using bitwise shifting and bit masking.Swapping two Bytes/Words using C program/* C program to swap bytes/words of integer number.*/ #include <stdio.h> int main() { unsigned int data = 0x...
In this program, we will read an integer variable and swap bytes of the given number using bitwise operators.Source CodeThe source code to swap bytes of an integer number is given below. The given program is compiled and executed successfully....
Scala code to swap two number using third variable objectmyObject{defmain(args:Array[String]):Unit={vara=10varb=20println("Values before swapping:\t a= "+a+", b= "+b)// swappingvartemp=a a=b b=temp println("Values after swapping:\t a= "+a+", b= "+b)}} ...
Java program to count the number of leading zeros in a binary number Java program to swap two bits of a 32-bit integer number Java program to check a given number is the power of 2 using bitwise operator Java program to count the number of bits to be flipped to convert a number to ...