TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
set_kth_bit.cpp File Reference

Implementation to [From the right, set the Kth bit in the binary representation of N] (https://practice.geeksforgeeks.org/problems/set-kth-bit3724/1/) in an integer. More...

#include <cassert>
#include <cstdint>
#include <iostream>
Include dependency graph for set_kth_bit.cpp:

Go to the source code of this file.

Namespaces

namespace  bit_manipulation
 for assert
 
namespace  setKthBit
 Functions for the [From the right, set the Kth bit in the binary representation of N] (https://practice.geeksforgeeks.org/problems/set-kth-bit3724/1/) implementation.
 

Functions

std::uint64_t bit_manipulation::set_kth_bit::setKthBit (std ::int64_t N, std ::int64_t k)
 The main function implements set kth bit.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation to [From the right, set the Kth bit in the binary representation of N] (https://practice.geeksforgeeks.org/problems/set-kth-bit3724/1/) in an integer.

Given a number N and a value K. From the right, set the Kth bit in the binary representation of N. The position of Least Significant Bit(or last bit) is 0, the second last bit is 1 and so on. in it.

A binary number consists of two digits. They are 0 & 1. Digit 1 is known as set bit in computer terms. Worst Case Time Complexity: O(1) Space complexity: O(1)

Author
Aman Raj

Definition in file set_kth_bit.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 77 of file set_kth_bit.cpp.

77 {
78 test(); // run self-test implementations
79 return 0;
80}
static void test()
Self-test implementations.

◆ setKthBit()

std::uint64_t bit_manipulation::set_kth_bit::setKthBit ( std ::int64_t N,
std ::int64_t k )

The main function implements set kth bit.

Parameters
Nis the number whose kth bit will be set
Returns
returns an integer after setting the K'th bit in N

Definition at line 43 of file set_kth_bit.cpp.

44 { // int64_t is preferred over int so
45 // that no Overflow can be there.
46
47 int pos =
48 1 << k; // "pos" variable is used to store 1 at kth postion and
49 // rest bits are 0. in binary representation of number 'n'
50
51 return N | pos; // by taking or with the pos and the N we set the bit of N
52 // at kth position.
53}
double k(double x)
Another test function.
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 61 of file set_kth_bit.cpp.

61 {
62 // n = 10,2 return 14
63 assert(bit_manipulation::set_kth_bit::setKthBit(10, 2) == 14);
64 // n = 25,1 return 27
65 assert(bit_manipulation::set_kth_bit::setKthBit(25, 1) == 27);
66 // n = 400001,5 return 400033
67 assert(bit_manipulation::set_kth_bit::setKthBit(400001, 5) == 400033);
68 // n = 123 return 123
69 assert(bit_manipulation::set_kth_bit::setKthBit(123, 3) == 123);
70
71 std::cout << "All test cases successfully passed!" << std::endl;
72}