A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d5/db5/set__kth__bit_8cpp.html below:

TheAlgorithms/C++: bit_manipulation/set_kth_bit.cpp File Reference

Loading...

Searching...

No Matches

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>

Go to the source code of this file.

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)

Definition in file set_kth_bit.cpp.

◆ main()

Main function.

Returns
0 on exit

Definition at line 77 of file set_kth_bit.cpp.

77 {

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
N is 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 {

45

46

47 int pos =

48 1 << k;

49

50

51 return N | pos;

52

53}

◆ test()

Self-test implementations.

Returns
void

Definition at line 61 of file set_kth_bit.cpp.

61 {

62

64

66

68

70

71 std::cout << "All test cases successfully passed!" << std::endl;

72}

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

The main function implements set kth bit.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4