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);
}
}
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);
}
}