×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to get total bits required for the given number using library function

Last Updated : December 15, 2025

Problem Solution

In this program, we will create some variables. Then we will get the total number of bits are required for a number using library function bit_length().

Program/Source Code

The source code to get the total bits required for the given number using the library function is given below. The given program is compiled and executed successfully.

# Ruby program to get total bits required for 
# given number using library function

num1 = 22;
num2 = 8;
num3 = 3;
num4 = 0;

print "Total bits required for num1: ",num1.bit_length(),"\n";
print "Total bits required for num2: ",num2.bit_length(),"\n";
print "Total bits required for num3: ",num3.bit_length(),"\n";
print "Total bits required for num4: ",num4.bit_length(),"\n";

Output

Total bits required for num1: 5
Total bits required for num2: 4
Total bits required for num3: 2
Total bits required for num4: 0

Explanation

In the above program, we created some variables. Then we got the total number of bits required for a number using library function bit_length() and printed the result.

Ruby Basic Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.