×

Challenges

  1. Not started

  2. Not started

  3. Not started

  4. Not started

  5. Not started

  6. Not started

  7. Not started

  8. Not started

  9. Not started

  10. Not started

  11. Not started

  12. Not started

  13. Not started

Challenge 6.1

Display the number of bits needed to represent a number

Challenge Level: Ready to expand

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

Output statements

Output a string directly
Blocks showing how to output a string
Output a variable
Blocks showing how to output a variable

Variables

Set a variable as a string
Blocks showing how to set a variable as a string
Set a variable as an integer
Blocks showing how to set a variable as an integer
Set a variable from a calculation
Blocks showing how to set a variable from a calculation
Set a variable from a user input (text)
Blocks showing how to set a variable from a user input (text)
Set a variable from a user input (number)
Blocks showing how to set a variable from a user input (number)
Add one to a value
Blocks showing how to add one to a value

Conditionals

Find out the discount on fruit
Blocks showing how to use conditional blocks

Loops

Output string 10 times
Blocks showing how to output a string
Output number until number reaches 10
Blocks showing how to output a string
Recommended Blocks
Number block
String block
Ask and wait for number block
Say block
Multiply block
Less than block
Join string block
Set variable to block
Change variable by block
Custom variable block
Repeat until block

Create your program in the editor below

Your submission results will be displayed here

Input Expected output Received output Status
5
3 bits

                
Not yet run ?
31
5 bits

                
Not yet run ?
32
6 bits

                
Not yet run ?
1
1 bits

                
Not yet run ?