mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add SwapAdjacentBits algorithm (#5634)
This commit is contained in:

committed by
GitHub

parent
c0f35242a1
commit
6535d4755d
@ -0,0 +1,26 @@
|
||||
package com.thealgorithms.bitmanipulation;
|
||||
|
||||
/**
|
||||
* Swap every pair of adjacent bits of a given number.
|
||||
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
|
||||
*/
|
||||
|
||||
public final class SwapAdjacentBits {
|
||||
private SwapAdjacentBits() {
|
||||
}
|
||||
|
||||
public static int swapAdjacentBits(int num) {
|
||||
// mask the even bits (0xAAAAAAAA => 10101010...)
|
||||
int evenBits = num & 0xAAAAAAAA;
|
||||
|
||||
// mask the odd bits (0x55555555 => 01010101...)
|
||||
int oddBits = num & 0x55555555;
|
||||
|
||||
// right shift even bits and left shift odd bits
|
||||
evenBits >>= 1;
|
||||
oddBits <<= 1;
|
||||
|
||||
// combine shifted bits
|
||||
return evenBits | oddBits;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user