Posts

Showing posts with the label DSA-Level1

Count set bits in an integer

Image
Introduction In this article, we will address the problem of "Counting set bits" or, alternatively, "Counting the number of set bits in an integer". Our approach will begin with a comprehensive understanding of the problem, followed by a brute-force solution, and eventually optimizing it with logical thinking. We hope this problem-solving journey will help boost your critical thinking and programming abilities. Without further ado, let's begin. Contents [ hide ] Understand the question The question "Count set bits in an integer" is asking for the number of 1s or set bits in the binary representation of a given integer. The binary representation of an integer is a sequence of 0s and 1s that represent the number in base 2. For example, the decimal number 7 is represented in binary as 111, which has three set bits. Similarly, the decimal number 10 is represented in binary as 1010, which has two set bits. The task is to coun...

Bit Manipulation and Bit Masking Concepts

Image
Introduction- A bit is the fundamental unit of information used in computing and digital communication.We generally worked on decimal numbers having values from 0,1,2.. to 9. Similarly binary system is characterized by two possible values, usually expressed as 0 or 1. In terms of electronic switch we can say 0 denotes an OFF switch and 1 denotes an ON switch. In terms of magnetic domain on a hard drive, we can say 0 denotes an unmagnetized domain, and 1 denotes a magnetized domain. Contents [ hide ] Uses of bit manipulation concepts in our coding Bit manipulation is a technique that can give programmers a significant performance advantage in certain algorithms. It can help to reduce the time complexity. By working directly with binary data using bitwise operators, we can perform operations faster than with traditional arithmetic operations. The reason for this is simple: computers understand binary data. When we perform any operation, it g...