Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Saturday, September 30, 2017

Swap Two Nibbles in Byte


       â€œx & 0x0F” gives us last 4 bits of x
The expression “x & 0xF0” gives us first four bits of x

Ref : http://practice.geeksforgeeks.org/problems/swap-two-nibbles-in-a-byte/0

CODE:

public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
    int n=ab.nextInt();
    System.out.println((n&0x0F)<<4 | (n&0xF0)>>4);
}
}

Stats

472796