Professional Documents
Culture Documents
FLIPPING A BIT
URL:https://forms.gle/6Lbibw3beTrJCA8z6
QR CODE:
SWAP TWO NIBBLES
IN A BYTE
Swap two nibbles in a byte
Introduction
• The concept of swapping two nibbles in a byte refers to
interchanging the values of the four least significant bits
(the lower nibble) with the four most significant bits (the
upper nibble) within a byte.
• In a binary representation of a byte, the nibbles are
represented by the four bits on the left (the upper nibble) and
the four bits on the right (the lower nibble).
• Swapping the nibbles means exchanging the values of the upper
and lower nibbles while keeping the rest of the bits unchanged.
Swap two nibbles in a byte
Example:N=100
N=100
100 in binary 0 1 1 0 0 1 0 0
After swapping
the nibbles
0 1 0 0 0 1 1 0
Swap two nibbles in a byte
Sample IO
Input: 100
Output: 70
Input: 200
Output: 140
Input: 67
Output: 52
Swap two nibbles in a byte
Program
import java.util.Scanner;
public class SwapNibble {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int swapnum;
swapnum = ((num & 0x0F) << 4 | (num & 0xF0) >> 4);
System.out.println("Before swapping the nibble: " + num);
System.out.println("\n After swapping the nibble: " +
swapnum);
// Close the scanner
scanner.close();
}
}
Interview questions
What is a nibble?
https://learn.codemithra.com