I have to calculate the maximum block in a binary number.
For ex:
Binary representation = 11111
Maximum block length = 5
Binary representation = 10111011
Maximum block length = 3
Above are the only 2 examples my professor gave.
"Compute the maximum block length of the binary
representation." This is what he said. I'm assuming this includes 0s as well. I really don't know how to go about it.
3 replies to this topic
#1
Posted 17 May 2011 - 07:41 AM
|
|
|
#2
Posted 17 May 2011 - 08:36 AM
The way I would do this is to set up a loop that examines one bit at the end at a time, and compares it with the value of the last bit from the previous operation. If it's a match, it increments a counter variable to keep track of how many similar bits it's encountered. If it's not a match, it checks the counter variable to see if you've reached a new maximum (another variable is needed to retain the current maximum) and then resets the counter to 1. After the comparison, you can perform a bitwise shift to push the examined bit off the end so the loop will examine the next bit in line. This entire loop will need to be constrained from the start to only run for as many times as there are bits in your input.
Another option is instead of using a bitwise shift to read the next bit, you could alternatively use a placeholder variable to mark which bit is currently being examined, and increment the placeholder each time. It's your choice. Either would work.
Another option is instead of using a bitwise shift to read the next bit, you could alternatively use a placeholder variable to mark which bit is currently being examined, and increment the placeholder each time. It's your choice. Either would work.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#3
Posted 17 May 2011 - 09:46 AM
I'd like more clarification please.
#4
Posted 17 May 2011 - 10:24 AM
Please take a look at this tutorial. Though it solves a different problem. But still it has the CODE and certain aspects common from the approach gregwarner has described above. You basically need to modify this code according to the approach he explained.
http://forum.codecal...ts-integer.html
http://forum.codecal...ts-integer.html
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









