You are on page 1of 2

Swap the nibbles of byte

Input: 0111 0110=118

Output:0110 0111=103

5 steps:

1)right nibble extract

2)left nibble extract

3)R>>4

4)L<<4

5)Add the resultant output

1)0111 0110

0X0F & 0111 0110

=>0111 0110

=>0000 1111

=>O/P=>0000 0110

3) 0000 0110 (LEFT<<4)

=>0110 0000

4) 0111 0110 &0XF0

=>0111 0110

=>1111 0000

=>O/P=>0111 0000

5) 0111 0000 (RIGHT>>4)

=>0000 0111

0110 0000

0000 0111

=>O/P=>0110 0111
import java.util.*;
public class Main
{
public static void main(String[] args) {

Scanner input=new Scanner(System.in);


int n= input.nextInt();//118
System.out.println("Before Nibble Swap "+n);//118
n=(n&0x0F)<<4 | (n&0xF0)>>4;
System.out.println("After Nibble Swap "+n);//103
}
}

You might also like