Posted on by Kalkicode
Code Bit Logic

Rotate bits of a number

Rotating bits of a number means shifting the bits of the number left or right by a certain number of positions, and then filling in the empty positions with either 0s or 1s. This is a common operation in computer programming and can be used for a variety of purposes, such as optimizing code or manipulating data.

To rotate the bits of a number, you can use bitwise operators such as the left shift operator (<<) or the right shift operator (>>). The left shift operator moves the bits of a number to the left by a certain number of positions, while the right shift operator moves the bits to the right.

Code Solution

``````// C program for
// Rotate bits of a number
#include <stdio.h>

void leftRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 *sizeof(int));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
printf("\n Given Number   : %d", num);
printf("\n Given Rotation : %d", k);
// Calculated left rotation
int result = (num << k) | (num >> (bytes - k));
// Display calculated result
printf("\n Result : %d", result);
}
void rightRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 *sizeof(int));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
printf("\n Given Number   : %d", num);
printf("\n Given Rotation : %d", k);
// Calculated right rotation
int result = (num >> k) | (num << (bytes - k));
// Display calculated result
printf("\n Result : %d", result);
}
int main(int argc, char
const *argv[])
{
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305
rightRotate(1815, 4);
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040
leftRotate(1815, 4);
return 0;
}``````

input

`````` Given Number   : 1815
Given Rotation : 4
Result : 1879048305
Given Number   : 1815
Given Rotation : 4
Result : 29040``````
``````/*
Java program
Count tree paths whose permutation is palindrome
*/

public class Rotation
{
public void leftRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 * Integer.BYTES);
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
System.out.print("\n Given Number : " + num );
System.out.print("\n Given Rotation : " + k );
// Calculated left rotation
int result = (num << k) | (num >> (bytes - k));
// Display calculated result
System.out.print("\n Result : " + result );
}
public void rightRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 * Integer.BYTES);
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
System.out.print("\n Given Number : " + num );
System.out.print("\n Given Rotation : " + k );
// Calculated right rotation
int result = (num >> k) | (num << (bytes - k));
// Display calculated result
System.out.print("\n Result : " + result );
}
public static void main(String[] args)
{

// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305

// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040

}
}``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 1879048305
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Count tree paths whose permutation is palindrome
*/

class Rotation
{
public: void leftRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 *sizeof(int));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
cout << "\n Given Number : " << num;
cout << "\n Given Rotation : " << k;
// Calculated left rotation
int result = (num << k) | (num >> (bytes - k));
// Display calculated result
cout << "\n Result : " << result;
}
void rightRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 *sizeof(int));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
cout << "\n Given Number : " << num;
cout << "\n Given Rotation : " << k;
// Calculated right rotation
int result = (num >> k) | (num << (bytes - k));
// Display calculated result
cout << "\n Result : " << result;
}
};
int main()
{
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040
return 0;
}``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 1879048305
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````// Include namespace system
using System;
/*
Csharp program
Count tree paths whose permutation is palindrome
*/
public class Rotation
{
public void leftRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 * sizeof(int));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
Console.Write("\n Given Number : " + num);
Console.Write("\n Given Rotation : " + k);
// Calculated left rotation
int result = (num << k) | (num >> (bytes - k));
// Display calculated result
Console.Write("\n Result : " + result);
}
public void rightRotate(int num, int k)
{
// Get number of bits in integer
int bytes = (8 * sizeof(int));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
Console.Write("\n Given Number : " + num);
Console.Write("\n Given Rotation : " + k);
// Calculated right rotation
int result = (num >> k) | (num << (bytes - k));
// Display calculated result
Console.Write("\n Result : " + result);
}
public static void Main(String[] args)
{
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040
}
}``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 1879048305
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````<?php
/*
Php program
Count tree paths whose permutation is palindrome
*/
class Rotation
{
public	function leftRotate(\$num, \$k)
{
// Get number of bits in integer
\$bytes = (8 * PHP_INT_SIZE);
if (\$k <= 0 || \$bytes < \$k)
{
// Invalid rotation
return;
}
echo("\n Given Number : ".\$num);
echo("\n Given Rotation : ".\$k);
// Calculated left rotation
\$result = (\$num << \$k) | (\$num >> (\$bytes - \$k));
// Display calculated result
echo("\n Result : ".\$result);
}
public	function rightRotate(\$num, \$k)
{
// Get number of bits in integer

\$bytes = (8 * PHP_INT_SIZE);
if (\$k <= 0 || \$bytes < \$k)
{
// Invalid rotation
return;
}
// Display given data
echo("\n Given Number : ".\$num);
echo("\n Given Rotation : ".\$k);
// Calculated right rotation
\$result = (\$num >> \$k) | (\$num << (\$bytes - \$k));
// Display calculated result
echo("\n Result : ".\$result);
}
}

function main()
{
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   Php is support 64 bit integer
//   0000000000000000000000000000000000000000000000000000011100010111
//                                                               ----
//                                                         ↆ      ↆ
// 000000000000000000000000000000000000000000000000000001110001 0111
//          ↘  ↙
//          ↙  ↘
//          000000000000000000000000000000000000000000000000000001110001
//                     ----------------
//  0111000000000000000000000000000000000000000000000000000001110001
//  = 8070450532247928945
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   0000000000000000000000000000000000000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   000000000000000000000000000000000000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000000000000000000000000000000000000111000101110000
//   ----------------
//  0000000000000000000000000000000000000000000000000111000101110000 = 29040
}
main();``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 8070450532247928945
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````/*
Node JS program
Count tree paths whose permutation is palindrome
*/
class Rotation
{
leftRotate(num, k)
{
// Get number of bits in integer
var bytes = ( Math.log2(Number.MAX_SAFE_INTEGER));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
process.stdout.write("\n Given Number : " + num);
process.stdout.write("\n Given Rotation : " + k);
// Calculated left rotation
var result = (num << k) | (num >> (bytes - k));
// Display calculated result
process.stdout.write("\n Result : " + result);
}
rightRotate(num, k)
{
// Get number of bits in integer
var bytes = ( Math.log2(Number.MAX_SAFE_INTEGER));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
process.stdout.write("\n Given Number : " + num);
process.stdout.write("\n Given Rotation : " + k);
// Calculated right rotation
var result = (num >> k) | (num << (bytes - k));
// Display calculated result
process.stdout.write("\n Result : " + result);
}
}

function main()
{

}
main();``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 237895793
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````import sys
#  Python 3 program
#  Count tree paths whose permutation is palindrome
class Rotation :
def leftRotate(self, num, k) :
#  Get number of bits in integer
bytes = (8 * sys.getsizeof(int()))
if (k <= 0 or bytes < k) :
#  Invalid rotation
return

print("\n Given Number : ", num, end = "")
print("\n Given Rotation : ", k, end = "")
#  Calculated left rotation
result = (num << k) | (num >> (bytes - k))
#  Display calculated result
print("\n Result : ", result, end = "")

def rightRotate(self, num, k) :
#  Get number of bits in integer
bytes = (8 * sys.getsizeof(int()))
if (k <= 0 or bytes < k) :
#  Invalid rotation
return

#  Display given data
print("\n Given Number : ", num, end = "")
print("\n Given Rotation : ", k, end = "")
#  Calculated right rotation
result = (num >> k) | (num << (bytes - k))
#  Display calculated result
print("\n Result : ", result, end = "")

def main() :
#  Test Case A
#  ----------------
#  number : 1815
#  binary : 11100010111
#  k      : 4 [Right Rotatation]
#    00000000000000000000011100010111
#                                ----
#                          ↆ       ↆ
#  0000000000000000000001110001  0111
#                            ↘  ↙
#                           ↙  ↘
#                        0111  0000000000000000000001110001
#                      ----------------
#   01110000000000000000000001110001 = 1879048305
#  Test Case B
#  ----------------
#  number : 1815
#  binary : 11100010111
#  k      : 4 [Left Rotatation]
#    00000000000000000000011100010111
#    ----
#      ↆ       ↆ
#    0000   0000000000000000011100010111
#    ----
#         ↘             ↙
#            ↘        ↙
#               ↘   ↙
#                 ↙
#               ↙    ↘
#             ↙        ↘
#           ↙            ↘
#    0000000000000000011100010111  0000
#    ----------------
#   00000000000000000111000101110000 = 29040

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

input

`````` Given Number :  1815
Given Rotation :  4
Result :  712058728107926599147622362695119659076610945731388915056753
Given Number :  1815
Given Rotation :  4
Result :  29040``````
``````#  Ruby program
#  Count tree paths whose permutation is palindrome
class Rotation
def leftRotate(num, k)
#  Get number of bits in integer
bytes = (8 * (0. size))
if (k <= 0 || bytes < k)
#  Invalid rotation
return
end

print("\n Given Number : ", num)
print("\n Given Rotation : ", k)
#  Calculated left rotation
result = (num << k) | (num >> (bytes - k))
#  Display calculated result
print("\n Result : ", result)
end

def rightRotate(num, k)
#  Get number of bits in integer
bytes = (8 * (0. size))
if (k <= 0 || bytes < k)
#  Invalid rotation
return
end

#  Display given data
print("\n Given Number : ", num)
print("\n Given Rotation : ", k)
#  Calculated right rotation
result = (num >> k) | (num << (bytes - k))
#  Display calculated result
print("\n Result : ", result)
end

end

def main()
#  Test Case A
#  ----------------
#  number : 1815
#  binary : 11100010111
#  k      : 4 [Right Rotatation]
#    00000000000000000000011100010111
#                                ----
#                          ↆ       ↆ
#  0000000000000000000001110001  0111
#                            ↘  ↙
#                           ↙  ↘
#                        0111  0000000000000000000001110001
#                      ----------------
#   01110000000000000000000001110001 = 1879048305
#  Test Case B
#  ----------------
#  number : 1815
#  binary : 11100010111
#  k      : 4 [Left Rotatation]
#    00000000000000000000011100010111
#    ----
#      ↆ       ↆ
#    0000   0000000000000000011100010111
#    ----
#         ↘             ↙
#            ↘        ↙
#               ↘   ↙
#                 ↙
#               ↙    ↘
#             ↙        ↘
#           ↙            ↘
#    0000000000000000011100010111  0000
#    ----------------
#   00000000000000000111000101110000 = 29040
end

main()``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 2092552530861427261553
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````/*
Scala program
Count tree paths whose permutation is palindrome
*/
class Rotation()
{
def leftRotate(num: Int, k: Int): Unit = {
// Get number of bits in integer
var bytes: Int = (8 * (4));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
print("\n Given Number : " + num);
print("\n Given Rotation : " + k);
// Calculated left rotation
var result: Int = (num << k) | (num >> (bytes - k));
// Display calculated result
print("\n Result : " + result);
}
def rightRotate(num: Int, k: Int): Unit = {
// Get number of bits in integer
var bytes: Int = (8 * (4));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
print("\n Given Number : " + num);
print("\n Given Rotation : " + k);
// Calculated right rotation
var result: Int = (num >> k) | (num << (bytes - k));
// Display calculated result
print("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Rotation = new Rotation();
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040
}
}``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 1879048305
Given Number : 1815
Given Rotation : 4
Result : 29040``````
``````/*
Swift 4 program
Count tree paths whose permutation is palindrome
*/
class Rotation
{
func leftRotate(_ num: Int, _ k: Int)
{
// Get number of bits in integer
let bytes = (8 * MemoryLayout < Int > .size);
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
print("\n Given Number : ", num, terminator: "");
print("\n Given Rotation : ", k, terminator: "");
// Calculated left rotation
let result = (num << k) | (num >> (bytes - k));
// Display calculated result
print("\n Result : ", result, terminator: "");
}
func rightRotate(_ num: Int, _ k: Int)
{
// Get number of bits in integer
let bytes = (8 * MemoryLayout < Int > .size);
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
print("\n Given Number : ", num, terminator: "");
print("\n Given Rotation : ", k, terminator: "");
// Calculated right rotation
let result = (num >> k) | (num << (bytes - k));
// Display calculated result
print("\n Result : ", result, terminator: "");
}
}
func main()
{
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040
}
main();``````

input

`````` Given Number :  1815
Given Rotation :  4
Result :  8070450532247928945
Given Number :  1815
Given Rotation :  4
Result :  29040``````
``````/*
Kotlin program
Count tree paths whose permutation is palindrome
*/
class Rotation
{
fun leftRotate(num: Int, k: Int): Unit
{
// Get number of bits in integer
val bytes: Int = (8 * (4));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
print("\n Given Number : " + num);
print("\n Given Rotation : " + k);
// Calculated left rotation
val result: Int = (num shl k) or(num shr(bytes - k));
// Display calculated result
print("\n Result : " + result);
}
fun rightRotate(num: Int, k: Int): Unit
{
// Get number of bits in integer
val bytes: Int = (8 * (4));
if (k <= 0 || bytes < k)
{
// Invalid rotation
return;
}
// Display given data
print("\n Given Number : " + num);
print("\n Given Rotation : " + k);
// Calculated right rotation
val result: Int = (num shr k) or(num shl(bytes - k));
// Display calculated result
print("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case A
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Right Rotatation]
//   00000000000000000000011100010111
//                               ----
//                         ↆ       ↆ
// 0000000000000000000001110001  0111
//                           ↘  ↙
//                          ↙  ↘
//                       0111  0000000000000000000001110001
//                     ----------------
//  01110000000000000000000001110001 = 1879048305
// Test Case B
// ----------------
// number : 1815
// binary : 11100010111
// k      : 4 [Left Rotatation]
//   00000000000000000000011100010111
//   ----
//     ↆ       ↆ
//   0000   0000000000000000011100010111
//   ----
//        ↘             ↙
//           ↘        ↙
//              ↘   ↙
//                ↙
//              ↙    ↘
//            ↙        ↘
//          ↙            ↘
//
//
//   0000000000000000011100010111  0000
//   ----------------
//  00000000000000000111000101110000 = 29040
}``````

input

`````` Given Number : 1815
Given Rotation : 4
Result : 1879048305
Given Number : 1815
Given Rotation : 4
Result : 29040``````

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