This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hello all, I had one query. When you append an int to a string, doesn't the variable get assigned a string. For instance I am having a hard time with the following code.
I am using bit shifting, my output looks great for this program; I have to implement, however, when I try to count how many 0s and 1s it get way off a way off answer. Is there something I am missing? The out put is the following.
I am basically looking on get the following numbers 26 5 4 which correspond to the following: total # of 1 bits, longest 1 bit sequence, longest 0 bit sequence. Thus, the answer should be 26 5 4. I have tried many different ways to implement this and I keep on gettting the same end result. Am I missing the big picture? Please correct me if I am wrong. You enter a key on the keyboard, which corresponds to a ascii code, which then corresponds to a bit pattern, which the computer really works with. I know java uses unicode(could this have something to do with why I am getting this kind of result). Any suggestions much appreciated. Thanks
If i am understanding you correctly this might be the case.
If i am looking at your loop where you count i get the impression you are doing this: When i add 1 + 2 + 3 + 4 + ... + 26 i get 351 does that suggest where you might have got it wrong?
Joined: Sep 23, 2005
Hi, I don't understand what you are saying. I am incrementing by 1 if a 1 is found in the string and incrementing by 1 if a zero character is fount. I am also keeping track of the total 1s in the string. I think my logic is correct here. If not please point out where I am missing out. Thanks
Joined: Sep 23, 2005
Actually, its late here. I see what you know mean. I just have to think of a way to count consective 1s and 0s. Cheers. Thanks again
Jan-Jaap van Nieuwkerk
Joined: Jul 03, 2006
Edit: you posted your answer while i was writing this. good luck
Hi i'll try to say it in another way.
You try to add 1 to countTotalOne by doing the following: countTotalOne+=countOne if you change that to adding 1 to CountTotalOne instead of the valy of o you get 26 instead of 351 as output.
As i said, look at the counters in your loop and what value you are actualy storing in your counters.
With a change to the abovementioned line i get this output
de ad be ef 42 1101111010101101101111101110111101000010 Total: 40 26 26 14
Now the first number is correct. The other 2 numbers (26 14) suggest that you do not reset your counters for longest #1 sequence and longest #0 sequence. You are at the moment counting all ones and zeroes that are in your string. try to reset your 1 and 0 counter the moment you find a longer string then the one you have found before.
Good Luck. [ September 29, 2006: Message edited by: Jan-Jaap van Nieuwkerk ]