ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.