Posted on by Kalkicode
Code Bit Logic

# Swap two nibbles in a byte

In this problem, we are given a byte (8 bits) and we need to swap its two nibbles. A nibble is a group of 4 bits. The goal is to interchange the positions of the first four bits with the last four bits.

## Problem Statement:

Given an 8-bit number (byte), we need to swap its two nibbles and obtain the result.

## Example:

Let's take the byte `175` (binary: `10101111`) as an example. The first nibble is `1010` and the second nibble is `1111`. We need to swap them to get `11111010`, which is the result `250`.

``` (175)   10101111
4 (bit) 1010 1111
↘ ↙
1111 1010
Result  (11111010) (250)```

## Pseudocode

``````swapNibbles(num):
// Get the first nibble (lower 4 bits)
firstNibble = num & 15   // 15 in binary: 00001111

// Get the second nibble (upper 4 bits)
secondNibble = (num & 240) >> 4  // 240 in binary: 11110000

// Swap the nibbles by combining them in reverse order
result = (firstNibble << 4) | secondNibble

return result
``````

## Algorithm Explanation

1. Create a function `swapNibbles(num)` that takes an integer `num` as input and returns an integer as output.
2. To get the first nibble, perform a bitwise AND operation between `num` and `15`. This can be done by using the binary mask `00001111`, where the lower 4 bits are set to 1 and the upper 4 bits are set to 0. This will extract the lower 4 bits of the number as the first nibble.
3. To get the second nibble, perform a bitwise AND operation between `num` and `240` and then shift the result 4 positions to the right. The binary mask `11110000` extracts the upper 4 bits of the number, and shifting it 4 positions to the right gives us the second nibble.
4. Now, we have the first nibble and the second nibble. To swap them, we shift the first nibble 4 positions to the left (`firstNibble << 4`) and then combine it with the second nibble using the bitwise OR operation (`|`). This gives us the final result with the nibbles swapped.
5. Return the result.

## Code Solution

Here given code implementation process.

``````// C program
// Swap two nibbles in a byte
#include <stdio.h>

void swapNibbles(int num)
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
int result = ((num & 15) << 4) | ((num & 240) >> 4 );

printf("\n Number : %d",num);
printf("\n Result : %d",result);
}
int main(int argc, char const *argv[])
{

// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
swapNibbles(175);
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
swapNibbles(225);
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
swapNibbles(2);

return 0;
}``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````/*
Java program
Swap two nibbles in a byte
*/
public class BitSwap
{
// Interchange 4 bits of a number
public void swapNibbles(int num)
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
int result = ((num & 15) << 4) | ((num & 240) >> 4);
System.out.print("\n Number : " + num);
System.out.print("\n Result : " + result);
}
public static void main(String[] args)
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}
}``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Swap two nibbles in a byte
*/
class BitSwap
{
public:
// Interchange 4 bits of a number
void swapNibbles(int num)
{
// (  num &15 ) Get initial 4 bit set bits in number
// (( num &15) << 4) this is moving initial 4 bits in left side [A]
// (( num &240)) Get next 4 bit set bits in number
// (( num &240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
int result = ((num &15) << 4) | ((num &240) >> 4);
cout << "\n Number : " << num;
cout << "\n Result : " << result;
}
};
int main()
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
return 0;
}``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````// Include namespace system
using System;
/*
C# program
Swap two nibbles in a byte
*/
public class BitSwap
{
// Interchange 4 bits of a number
public void swapNibbles(int num)
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
int result = ((num & 15) << 4) | ((num & 240) >> 4);
Console.Write("\n Number : " + num);
Console.Write("\n Result : " + result);
}
public static void Main(String[] args)
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}
}``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````<?php
/*
Php program
Swap two nibbles in a byte
*/
class BitSwap
{
// Interchange 4 bits of a number
public	function swapNibbles(\$num)
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
\$result = ((\$num & 15) << 4) | ((\$num & 240) >> 4);
echo "\n Number : ". \$num;
echo "\n Result : ". \$result;
}
}

function main()
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}
main();``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````/*
Node Js program
Swap two nibbles in a byte
*/
class BitSwap
{
// Interchange 4 bits of a number
swapNibbles(num)
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
var result = ((num & 15) << 4) | ((num & 240) >> 4);
process.stdout.write("\n Number : " + num);
process.stdout.write("\n Result : " + result);
}
}

function main()
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}
main();``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````#   Python 3 program
#   Swap two nibbles in a byte

class BitSwap :
#  Interchange 4 bits of a number
def swapNibbles(self, num) :
#  (  num & 15 ) Get initial 4 bit set bits in number
#  (( num & 15) << 4) this is moving initial 4 bits in left side [A]
#  (( num & 240)) Get next 4 bit set bits in number
#  (( num & 240) >> 4 ) moving next 4 bits in right side [B]
#  A or B return swap version of 4 bits
result = ((num & 15) << 4) | ((num & 240) >> 4)
print("\n Number : ", num, end = "")
print("\n Result : ", result, end = "")

def main() :
#  (175)   10101111
#  4 (bit) 1010 1111
#             ↘ ↙
#          1111 1010
#  Result  (11111010) (250)
#  (225)   11100001
#  4 (bit) 1110 0001
#             ↘ ↙
#          0001 1110
#  Result  (00011110) (30)
#  (2)     00000010
#  4 (bit) 0000 0010
#             ↘ ↙
#          0010 0000
#  Result  (00100000) (32)

if __name__ == "__main__": main()``````

#### Output

`````` Number :  175
Result :  250
Number :  225
Result :  30
Number :  2
Result :  32``````
``````#   Ruby program
#   Swap two nibbles in a byte

class BitSwap
#  Interchange 4 bits of a number
def swapNibbles(num)
#  (  num & 15 ) Get initial 4 bit set bits in number
#  (( num & 15) << 4) this is moving initial 4 bits in left side [A]
#  (( num & 240)) Get next 4 bit set bits in number
#  (( num & 240) >> 4 ) moving next 4 bits in right side [B]
#  A or B return swap version of 4 bits
result = ((num & 15) << 4) | ((num & 240) >> 4)
print("\n Number : ", num)
print("\n Result : ", result)
end

end

def main()
#  (175)   10101111
#  4 (bit) 1010 1111
#             ↘ ↙
#          1111 1010
#  Result  (11111010) (250)
#  (225)   11100001
#  4 (bit) 1110 0001
#             ↘ ↙
#          0001 1110
#  Result  (00011110) (30)
#  (2)     00000010
#  4 (bit) 0000 0010
#             ↘ ↙
#          0010 0000
#  Result  (00100000) (32)
end

main()``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````/*
Scala program
Swap two nibbles in a byte
*/
class BitSwap
{
// Interchange 4 bits of a number
def swapNibbles(num: Int): Unit = {
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
var result: Int = ((num & 15) << 4) | ((num & 240) >> 4);
print("\n Number : " + num);
print("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitSwap = new BitSwap();
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}
}``````

#### Output

`````` Number : 175
Result : 250
Number : 225
Result : 30
Number : 2
Result : 32``````
``````/*
Swift 4 program
Swap two nibbles in a byte
*/
class BitSwap
{
// Interchange 4 bits of a number
func swapNibbles(_ num: Int)
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
let result: Int = ((num & 15) << 4) | ((num & 240) >> 4);
print("\n Number : ", num, terminator: "");
print("\n Result : ", result, terminator: "");
}
}
func main()
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}
main();``````

#### Output

`````` Number :  175
Result :  250
Number :  225
Result :  30
Number :  2
Result :  32``````
``````/*
Kotlin program
Swap two nibbles in a byte
*/
class BitSwap
{
// Interchange 4 bits of a number
fun swapNibbles(num: Int): Unit
{
// (  num & 15 ) Get initial 4 bit set bits in number
// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
// (( num & 240)) Get next 4 bit set bits in number
// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
// A or B return swap version of 4 bits
var result: Int = ((num and 15) shl 4) or((num and 240) shr 4);
print("\n Number : " + num);
print("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// (175)   10101111
// 4 (bit) 1010 1111
//            ↘ ↙
//         1111 1010
// Result  (11111010) (250)
// (225)   11100001
// 4 (bit) 1110 0001
//            ↘ ↙
//         0001 1110
// Result  (00011110) (30)
// (2)     00000010
// 4 (bit) 0000 0010
//            ↘ ↙
//         0010 0000
// Result  (00100000) (32)
}``````

#### Output

`````` Number :  175
Result :  250
Number :  225
Result :  30
Number :  2
Result :  32``````

## Time Complexity

The time complexity of this algorithm is O(1) because the operations involved (bitwise AND, OR, and shifting) are constant-time operations. The algorithm runs in constant time regardless of the size of the input (which is fixed at 8 bits in this case).

## Output Explanation

The program takes three test cases: `175`, `225`, and `2`, and swaps the nibbles for each of them.

1. For `175` (binary: `10101111`), the result is `250` (binary: `11111010`). The first nibble `1010` is swapped with the second nibble `1111`, giving us `11111010`, which is `250` in decimal.
2. For `225` (binary: `11100001`), the result is `30` (binary: `00011110`). The first nibble `1110` is swapped with the second nibble `0001`, giving us `00011110`, which is `30` in decimal.
3. For `2` (binary: `00000010`), the result is `32` (binary: `00100000`). The first nibble `0000` is swapped with the second nibble `0010`, giving us `00100000`, which is `32` in decimal.

The program successfully swaps the nibbles for each test case, and the output is as expected.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post