Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q 2 Groups of four bits [ 2 0 points ] A group of four bits is called a nibble. In this question your task
Q Groups of four bits points
A group of four bits is called a nibble. In this question your task is to write a function makenibbles that takes a string containing bits digits & and returns a new string where bits are grouped into nibbles separated by a space, adding extra zeros on left, if necessary, to make the string length a multiple of
Example:
nibbles makenibbles
printnibbles
printtypenibbles
Output:
Note that padding a binary number with zeros on left does not change its value. We are padding zeros just to make the length a multiple of so that we can make groups of four bits. This makes reading a long binary number a bit easier.
Logic
The function makenibbles should do the following:
First add extra zeros on the left of the argument string bits to create a paddedstring whose length is a multiple of Hint: you can use len function and remainder operator to figure out how many zeros should be added. For example, for zeros are need to make its length : but for only zero is needed:
Build a new string result using a loop basically copy each bit from paddedstring to result but after every th bit, add a space to result string. Hint: you can use remainder operator here as well for the "every th bit" part of the logic.
Return the result string
More examples
printmakenibbles #
printmakenibbles #
# No zero padding needed as length is already a multiple of :
printmakenibbles #
Assume that the length of argument string will be at least one ie it won't be an empty string. But there is no upper limit on the length.
It is okay if there are extra spaces at beginning or end of the result string eg or is fine. But extra spaces or incorrect spaces between bits are not acceptable eg or are incorrect.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started