id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

abraskin@mbusd.org chapter1exam > bitsRequired
prev  |  next  |  chance

The number of bits required to store a positive integer can be calculated by counting how many times it takes to divide the number in half for it to reach 0. For example, it requires x bits to store both the numbers 8 and 9 because they will each reach zero after they are divided in half x times: 8/2=4, 4/2=2, 2/2=1, and 1/2=0 requires 4 bits. 9/2=4, 4/2=2, 2/2=1, and 1/2=0 requires 4 bits. The only exception is that it still requires 1 bit to store the number 0! return the number of bits required to store a positive integer.


bitsRequired(0) → 1
bitsRequired(1) → 1
bitsRequired(4) → 3

...Save, Compile, Run (ctrl-enter)

public int bitsRequired(int n) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200

Copyright Nick Parlante 2017 - privacy