URL:https://forms.gle/6Lbibw3beTrJCA8z6
QR CODE:
TEST TIME ON LONGEST SEQUENCE OF
1’ AFTER FLIPPING A BIT
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.
Example:N=100
N=100
100 in binary
0 1 1 0 0 1 0 0
left (the upper
nibble)
right (the lower
nibble)
Swap left and right 0 1 0 0 0 1 1 0
0 1 0 0 0 1 1 0
After swapping
the nibbles
• 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.
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.
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
Program
Sample IO
Input: 100
Output: 70
Input: 200
Output: 140
Input: 67
Output: 52
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();
}
Program:
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?
Interview questions
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.
Is there any difference between swapping two nibbles in a byte and
swapping the bytes themselves?
Interview questions
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.
https://learn.codemithra.com
THANK YOU
+91 78150 95095 codemithra@ethnus.com www.codemithra.com

FALLSEM2025-26_BSTS301P_SS_CH2025260100067_2025-08-29_Reference-Material-I.pptx

  • 2.
    URL:https://forms.gle/6Lbibw3beTrJCA8z6 QR CODE: TEST TIMEON LONGEST SEQUENCE OF 1’ AFTER FLIPPING A BIT
  • 3.
  • 4.
    INTRODUCTION : • Theconcept 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.
  • 5.
    Example:N=100 N=100 100 in binary 01 1 0 0 1 0 0 left (the upper nibble) right (the lower nibble) Swap left and right 0 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 After swapping the nibbles
  • 6.
    • The purposeof 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.
  • 7.
    Procedure: The snippet ofcode 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.
  • 8.
    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
  • 9.
    Program Sample IO Input: 100 Output:70 Input: 200 Output: 140 Input: 67 Output: 52
  • 10.
    import java.util.Scanner; public classSwapNibble { 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(); } Program:
  • 11.
    What is anibble? A nibble is a four-bit aggregation, also known as a half-byte. It represents a single hexadecimal digit. Interview questions
  • 12.
    Explain the conceptof 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
  • 13.
    What is thesignificance of swapping two nibbles in a byte? Interview questions 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.
  • 14.
    Is there anydifference between swapping two nibbles in a byte and swapping the bytes themselves? Interview questions 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.
  • 15.
  • 16.
    THANK YOU +91 7815095095 codemithra@ethnus.com www.codemithra.com

Editor's Notes

  • #10 Enter a number: 100 Before swapping the nibble: 100 After swapping the nibble: 70