// 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...
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...
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...
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 ...
We can also swap two numbers without using the third variable. This method saves computation space hence is more effective. Let, variableacontains first value, variablebcontains second value. Step 1: a = a + b Step 2: a = a - b ...
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...
Java program to swap two numbers using bitwise operator Java program to find the highest bit set for a given integer number Java program to check if all the bits of a given integer number are HIGH or not Java program to count the total HIGH bits in the given number Java program to check...
#include<iostream>usingnamespacestd;unsignedintswap_odd_even(intnum){intmask=0xAAAAAAAA;//A in hexadecimal is equal to 10 in decimal//and 1010 in binaryunsignedintoddbits=(num&mask);//right shift for even bitsunsignedintevenbits=num&(mask>>1);//can also use 0x55555555 as mask for even...
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 ...