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

Ruby Example: Write a program to get total bits required for the given number using library function.
Submitted by Nidhi, on December 15, 2021

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 »




Comments and Discussions!

Load comments ↻





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