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.println("Second...
In the above program, we imported a packageSwiftto use theprint()function using the below statement, 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...
// 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...
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 ...
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...
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 without using third variable objectmyObject{defmain(args:Array[String]):Unit={vara=10varb=20println("Values before swapping:\t a= "+a+", b= "+b)// swappinga=a+b b=a-b a=a-b println("Values after swapping:\t a= "+a+", b= "+b)}} ...
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains two static methods swapTwoNibbles() and main().The swapTwoNibbles() method is used to swap two nibbles of a given byte using bitwise operators ...
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...
Rust program to find the total number of leading zeros in a binary number Rust program to read a number and print bits between given positions Rust program to swap two bits of a 32-bit integer number Rust program to check a given number is the power of 2 using bitwise operator Rust ...