Thursday, 19 December 2024

Day 47: Python Program to Set Bit of an Integer




def count_set_bits(n):

    count=0

    while n > 0:

        count += n & 1

        n >>= 1

        return count

num=int(input("Enter an integer"))

print(f"The number of set bits in {num}is{count_set_bits(num)}.")

#source code --> clcoding.com 

 Code Explanation:

Function: count_set_bits
def count_set_bits(n):
    count = 0
    while n > 0:
        count += n & 1
        n >>= 1
        return count

Initialization:
count = 0: Initializes a variable count to store the number of set bits.

Logic:
n & 1: This operation checks if the least significant bit (LSB) of n is 1.
& is a bitwise AND operator. It returns 1 only if both corresponding bits are 1.
For example:
5 (binary: 101) & 1 (binary: 001) = 1 (LSB is 1).
4 (binary: 100) & 1 (binary: 001) = 0 (LSB is 0).
count += n & 1: Adds 1 to count if the LSB of n is 1.
n >>= 1: Right-shifts n by 1 bit to examine the next bit.
For example:
If n = 5 (binary: 101), n >> 1 = 2 (binary: 10).
If n = 2 (binary: 10), n >> 1 = 1 (binary: 1).

Loop Condition:
The loop continues as long as n > 0, meaning there are more bits to check.
Return Value:
The function returns count, which is the total number of set bits in the binary representation of n.
num = int(input("Enter an integer"))
Prompts the user to input an integer (num).
Converts the input string to an integer using int().
print(f"The number of set bits in {num} is {count_set_bits(num)}.")
Calls the count_set_bits function with num as input.
Prints the number of set bits.

Input:
Enter an integer: 5
Execution of count_set_bits(5)
Initial value: n = 5 (binary: 101), count = 0.

First iteration:
n & 1: 101 & 001 = 1 (LSB is 1).
count += 1: count = 1.
n >>= 1: n = 2 (binary: 10).

Second iteration:
n & 1: 10 & 01 = 0 (LSB is 0).
count = 1 (no increment).
n >>= 1: n = 1 (binary: 1).

Third iteration:
n & 1: 1 & 1 = 1 (LSB is 1).
count += 1: count = 2.
n >>= 1: n = 0 (loop exits).
Return count = 2.

Output:
The number of set bits in 5 is 2.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses