

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to count total bits in a number
Let us say the number we have is 12. We have declared and initialized a uint variable by assigning a decimal literal,
uint val = 12;
The binary representation of 12 is −
1100
The bits above is 4, therefore to find the total bits, use the Math.log() method −
uint res = (uint)Math.Log(val , 2.0) + 1;
Example
You can try to run the following code to count total bits in a number.
using System; public class Demo { public static void Main() { uint val = 12; // 1100 in binary uint res = (uint) Math.Log(val, 2.0) + 1; // 1100 has 4 bits Console.WriteLine("Total bits: " + res); } }
Output
Total bits: 4
- Related Questions & Answers
- Java program to count total bits in a number
- C# program to count total set bits in a number
- Write a python program to count total bits in a number?
- Count total bits in a number in C++
- Python program to count total set bits in all number from 1 to n.
- Program to count total number of set bits of all numbers in range 0 to n in Python
- Count unset bits of a number in C++
- Python program to count unset bits in a range.
- How to count set bits in a floating point number in C?
- Write a program in Python to count the total number of leap years in a given DataFrame
- Java Program to Count set bits in an integer
- Python Program to Count set bits in an integer
- Write a Python program to count the total number of ages between 20 to 30 in a DataFrame
- Program to invert bits of a number Efficiently in C++
- How to count the total number of links in a page in Selenium?
Advertisements