×

Challenges

Challenge 6.1

# Display the number of bits needed to represent a number

## Requirement:

Write a program that asks the user to enter a number of dots (i.e. decimal value) as the input and displays how many bits will be needed to represent that number as the output. For example, the number 15 can be represented in 4 bits, but 16 requires 5 bits. You can work this out by starting with the number 1, and doubling it until it gives you a value more than the number you need to represent, so this program can be based on the earlier challenge to work out the number of dots on each card.

Hints
• Make variables called:

• "total number of dots" and set its value to the number entered by the end user.
• “bit value” and set its value to ‘1’. Double its value until it is bigger than the “total number of dots”.
• “bits” and set its value to 0. Every time that you double the “bit value” you need to add a new bit for storing the number (increase the value of “bits” by 1).
• Use 'Repeat until' block to repeat the blocks until “total number of dots” is greater than the “bit value”. Stop the loop when you find the “bit value” which is larger than the “total number of dots”. Display the value of “bits” as the output.

• Set the variable “bit value” to ‘1’ and double it (Use the 'Multiply' block under “Operators” to multiply the “bit value” by 2) until it is bigger than the “total number of dots”. Every time that you double the “bit value” you need to add a new bit for storing the number. You can do this by using the 'Change variable by' block to increase the value of variable “bits” value by 1. Use 'Repeat until' block to repeat these steps until condition is true. In this challenge you need to repeat the blocks until “total number of dots” is greater than the “bit value”. Stop the loop when you find the “bit value” is larger than the “total number of dots”.

• Test your program with some values on the boundaries (for example number 31 would need 5 bits and 32 needs 6 bits).

Programming Reminders

#### Loops

###### Output number until number reaches 10
Recommended Blocks

Create your program in the editor below

Your submission results will be displayed here

Input Expected output Received output Status
31
5 bits

Not yet run ?
32
6 bits

Not yet run ?
1
1 bits

Not yet run ?
5
3 bits

Not yet run ?