Hướng dẫn find first set bit c++ - tìm bit đặt đầu tiên c ++

Viết hàm một dòng để trả về vị trí của 1 đầu tiên từ phải sang trái, trong biểu diễn nhị phân của số nguyên. & NBSP;

Examples:

Đầu vào: N = 18Output: 2Explanation: Biểu diễn nhị phân là 18 là 010010, do đó vị trí của bit đầu tiên từ bên phải là 2. n = 18
Output: 2
Explanation: Binary Representation of 18 is 010010, hence position of first set bit from right is 2.

Đầu vào: & nbsp; n = 19Output: 1Explanation: Biểu diễn nhị phân của 19 là 010011, do đó vị trí của bit đầu tiên từ bên phải là 1.  n = 19
Output: 1
Explanation: Binary Representation of 19 is 010011, hence position of first set bit from right is 1.

Vị trí của bit đặt ngoài cùng bên phải bằng cách bổ sung hai từ:

. Ở đây log2 sẽ trả về, số lần chúng ta có thể thể hiện số đó trong sức mạnh của hai. Đối với tất cả các số nhị phân chỉ chứa bit đặt ngoài cùng bên phải là 1 như 2, 4, 8, 16, 32. Tìm vị trí đó của bit đặt ngoài cùng bên phải luôn bằng log2 [số] + 1. always return the binary number containing the rightmost set bit as 1. if N = 12 [1100] then it will return 4 [100]. Here log2 will return, the number of times we can express that number in a power of two. For all binary numbers containing only the rightmost set bit as 1 like 2, 4, 8, 16, 32…. Find that position of rightmost set bit is always equal to log2[Number] + 1.

Thực hiện theo các bước để giải quyết vấn đề đã cho:

  • Đặt đầu vào là 12 [1100]
  • Lấy hai phần bổ sung của không có vì tất cả các bit được hoàn nguyên ngoại trừ ‘1 1 từ phải sang trái [0100]
  • Làm một chút khôn ngoan và với bản gốc không, điều này sẽ trả về không với một điều cần thiết chỉ [0100]
  • Lấy log2 của không, bạn sẽ nhận được [vị trí - 1] [2]
  • Thêm 1 [3]

Dưới đây là việc thực hiện phương pháp trên:

C++

#include

#include

using namespace std;

class gfg {

public:

    ____1010____11 #include 2#include 1 #include 4

    #include 6

#include 7#include 8 #include 9

    using1

using2

#include 1 using4

#include 6

    using7

    #include 1 namespace0

    namespace2

    #include 8 namespace5

using1

C

#include

namespace8

#include 1 using4

#include 6

    #include 1 namespace0

    #include 8 namespace5

    std;9class0class1class2

    class4class5

    #include 8 namespace5

using1

using1

C

    #include 1 std;7

Java

    #include 6

gfg {0 gfg {1

:0:1:2:3

    using1

class gfg {3

    #include 6

    public gfg {6 #include 1 #include 2#include 1 #include 4

#include 7    9

    using1

using1

Python3

#include 7#include 8 class0#include 1__777778public9

    public gfg {6 :9     0

#include 7#include 1     5    6:3

gfg {0 #include 04

#include 05 #include 06

C#

    #include 8 #include 09#include 10#include 4#include 122

    #include 1 std;7

Java

    #include 6

gfg {0 gfg {1

:0#include 40

    using1

class gfg {3

    #include 6

    public gfg {6 #include 1 #include 2#include 1 #include 4

#include 7#include 54

    using1

using1

#include 7#include 8 class0#include 1__777778public9

    public gfg {6 :9     0

#include 7#include 1     5    6:3

#include 6

gfg {0 #include 04

#include 70#include 61#include 72

using1

#include 05 #include 06

    #include 8 #include 09#include 10#include 4#include 122

#include 80

#include 14#include 15     6

#include 81

#include 17class0____111120120

#include 6

using #include 22

using1

    #include 90

    #include 92

    #include 94

#include 95

#include 7#include 8 class0#include 1#include 38O[log2N], Time taken by log2 function.
Auxiliary Space: O[1]

    public gfg {6 :9 #include 47ffs[] function:

#include 7#include 1 namespace0
Illustration:

PHPN = 12

#include 58

#include 59 #include 2#include 61#include 623

Dưới đây là việc thực hiện phương pháp trên:

C++

#include 96

using namespace std;

class gfg {

#include 1 using4

#include 6

    #include 1 namespace0

    using13

    #include 8 namespace5

using1

using1

C

    #include 1 std;7

Java

    #include 6

gfg {0 gfg {1

:0:1:2:3

    using1

class gfg {3

    #include 6

    public gfg {6 #include 1 #include 2#include 1 #include 4

#include 7using56

    using1

using1

Python3

#include 7#include 8 class0#include 1__777778public9

    public gfg {6 :9     0

#include 7#include 1     5    6:3

gfg {0 #include 04

    #include 14#include 15     6

    #include 17using85

C#

using #include 22

public class gfg {3

    gfg {6 #include 1 #include 2#include 1 #include 4

    #include 6

#include 7#include 8 class0#include 1#include 38

:0#include 40

    using1

    public gfg {6 :9 namespace12

    #include 6

#include 7#include 1 namespace0

#include 7namespace19

    using1

using1

JavaScript

#include 81

#include 59 #include 83

#include 6

    #include 8 #include 87

using1

    #include 92

    namespace34

#include 95

Độ phức tạp về thời gian: O [log2n], thời gian được thực hiện bởi ffs [] function.AUXILIARY Không gian: O [1] O[log2N], Time taken by ffs[] function.
Auxiliary Space: O[1]

Vị trí của bit đặt ngoài cùng bên phải bằng cách sử dụng & nbsp; & nhà điều hành: & operator:

Thực hiện theo các bước dưới đây để giải quyết vấn đề:

  • Khởi tạo M là 1 khi kiểm tra XOR của nó với các bit bắt đầu từ bit ngoài cùng bên phải. & Nbsp;
  • Bỏ qua một lần cho đến khi chúng tôi tìm thấy bit & nbsp đầu tiên;
  • Vì bit tập đầu tiên cho một số khi chúng tôi thực hiện A & Hoạt động với m. & NBSP;

Dưới đây là việc thực hiện phương pháp trên:

C++

#include 96

using namespace std;

#include 1 namespace41#include 1 #include 4

#include 6

    using73 namespace47

#include 7#include 8 namespace5

    #include 1 namespace53

    #include 1 namespace56

    namespace58 namespace59

#include 7namespace61

#include 7namespace63

    using1

    #include 8 namespace68

using1

#include 1 using4

#include 6

    #include 1 namespace0

    namespace77

    #include 8 namespace5

using1

Java

class gfg {3

    gfg {6 #include 1 namespace41#include 1 #include 4

    #include 6

#include 7#include 1 namespace94:2:3

#include 7#include 1 namespace99:2:3

#include 7namespace58 std;04std;05std;06

:0std;08:2:3

:0namespace63

#include 7using1

#include 7#include 8 namespace68

    using1

    public gfg {6 :9     0

    #include 6

#include 7#include 1     5    6:3

#include 7std;33

    using1

using1

Python3

#include 05 std;38

    std;40#include 15 :2

    std;444#include 15 :2

    namespace58 class0std;50std;51

#include 7std;444#include 15 std;55:2

#include 7std;40#include 122____115 :2

    #include 8 std;64

#include 14#include 15     6

#include 17std;69

C#

using #include 22

class gfg {3

    gfg {6 #include 1 namespace41#include 1 #include 4

    #include 6

#include 7#include 1 namespace94:2:3

#include 7#include 1 namespace99:2:3

#include 7namespace58 std;04std;05std;06

:0namespace61

:0namespace63

#include 7using1

#include 7#include 8 namespace68

    using1

    using1

    #include 6

#include 7#include 1 namespace0

#include 7class13

    using1

using1

JavaScript

#include 58

#include 59 #include 83

#include 6

    #include 8 #include 87

Độ phức tạp về thời gian: O [log2n], thời gian được thực hiện bởi ffs [] function.AUXILIARY Không gian: O [1]

Vị trí của bit đặt ngoài cùng bên phải bằng cách sử dụng & nbsp; & nhà điều hành:

    #include 6

Thực hiện theo các bước dưới đây để giải quyết vấn đề:

#include 7class24class45

    using1

Khởi tạo M là 1 khi kiểm tra XOR của nó với các bit bắt đầu từ bit ngoài cùng bên phải. & Nbsp;

using1

Bỏ qua một lần cho đến khi chúng tôi tìm thấy bit & nbsp đầu tiên;

Vì bit tập đầu tiên cho một số khi chúng tôi thực hiện A & Hoạt động với m. & NBSP;

#include 80

JavaScript

#include 81

#include 59 #include 83

    #include 6

#include 7class67

#include 7class69

#include 7namespace58 std;04std;05std;06

:0namespace61

:0namespace63

#include 7using1

#include 7#include 8 namespace68

    using1

    #include 92

    using1

#include 95

    public gfg {6 :9     0O[log2N], Traversing through all the bits of N, where at max there are logN bits.
Auxiliary Space: O[1]

#include 7#include 1     5    6:3Left Shift[

Bài Viết Liên Quan

Chủ Đề