You are on page 1of 15

TEST TIME ON LONGEST SEQUENCE OF 1’ AFTER

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

left (the right (the


upper nibble) lower nibble)

Swap left and right


0 1 0 0 0 1 1 0

After swapping
the nibbles
0 1 0 0 0 1 1 0
Swap two nibbles in a byte

• The purpose of swapping nibbles in a byte can vary depending on


the context. It can be a data transformation technique used in
certain algorithms or programming tasks.

• Swapping nibbles can facilitate certain operations on the data


stored within a byte or enable efficient storage or transmission
of specific information.

• Overall, swapping two nibbles in a byte is a simple bitwise


operation that involves extracting the values of the nibbles,
storing them temporarily, and then recombining them in the
opposite order to create the swapped byte.
Swap two nibbles in a byte
Procedure
The snippet of code for nibble swap is as follows:
(x & 0x0F) << 4 | (x & 0xF0) >> 4
Explanation
As we know binary of 100 is 01100100. To swap the nibble we split
the operation into two parts, in the first part we get the last 4 bits
and in the second part, we get the first 4 bits of a byte.
First operation: The expression “data & 0x0F” gives us the last 4 bits
of data and the result would be 00000100.
Using the bitwise left shift operator ‘<<‘, we shift the last four bits
to the left 4 times and make the new last four bits as 0. The result
after the shift is 01000000.
Swap two nibbles in a byte
Procedure
Second operation:
The expression “data & 0xF0” gives us first four bits of
data and result would be 01100000.
Using the bitwise right shift operator ‘>>’ , we shift the digit to
the right 4 times and make the first four bits 0. The result after
the shift is 00000110.
After completing the two operations we use the bitwise OR
‘|’ operation on them. After OR operation you will find that first
nibble in the place of the last nibble and the last nibble to the
place of first nibble
Swap two nibbles in a byte
Program

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?

A nibble is a four-bit aggregation, also known as a


half-byte. It represents a single hexadecimal digit.
Interview questions

Explain the concept of swapping two nibbles in a byte.

Swapping two nibbles in a byte means interchanging the


positions of the first four bits (the first nibble)
with the last four bits (the second nibble) in a byte.
Interview questions

What is the significance of swapping two nibbles in a byte?

Swapping two nibbles in a byte can be useful in certain


applications, such as data encryption or data
compression algorithms. It allows for reordering or
rearranging the bits within a byte, which can impact
the representation or interpretation of data.
Interview questions

Is there any difference between swapping two nibbles in a byte and


swapping the bytes themselves?

Yes, there is a difference. Swapping two nibbles in a byte


involves interchanging the positions of the first four bits
with the last four bits within the same byte. On the other
hand, swapping the bytes themselves refers to interchanging
the positions of two entire bytes within a larger data
structure, such as a word or a memory block.
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like