Swap all odd and even bits

gfg, potd, java, easy, appris, edge_case, note

Problem Statement #

Given an unsigned integer N. The task is to swap all odd bits with even bits. For example, if the given number is 23 (00010111), it should be converted to 43(00101011). Here, every even position bit is swapped with adjacent bit on the right side(even position bits are highlighted in the binary representation of 23), and every odd position bit is swapped with an adjacent on the left side.

Appris #

Notes #

Solution #

class Solution{
    //Function to swap odd and even bits.
    public static int swapBits(int n) 
    {
	    // Your code
	    String s = Integer.toBinaryString(n);
	    StringBuilder sb = new StringBuilder(s);
	    if(s.length()%2==1 && sb.length()<31){
	        sb.insert(0,"0");
	    }
	    
	    for(int i=0;i<s.length();i+=2){
	        char temp = sb.charAt(i);
	        sb.setCharAt(i,sb.charAt(i+1));
	        sb.setCharAt(i+1,temp);
	    }
	    String e = sb.toString();
	    
	    int res = Integer.parseInt(e, 2);
	    return res;
	}
    
}