Debugging typos
Posted on:March 06 2008
I really want to bang my head against something when I spent an hour debugging a problem again and found it to be a simple typo. For example, I found the problem in the following code (not originally C++ but translated to C it would look like this). It simply should count the numbers of bits set in a value:
value = some value where we want to count the number of bits set in it.
testBit = 0x1;
bitsSet = 0;
for (int i=0; i<16; ++i)
{
if (value & testBit)
bitsSet += 1;
testBit << 1;
}
See the problem? Yep, it's the last line. Instead of 'testBit << 1;', it should read 'testBit = testBit << 1;'. Argh. Too bad the compiler didn't write a warning, because that line doesn't actually make sense at all, in any case.
Comments:
Michael
Quote
2008-03-06 19:22:00
Sylence
Quote
2008-03-06 19:25:00
Electron
Quote
2008-03-06 19:27:00
Electron
Quote
2008-03-06 19:31:00
BKLA
Quote
2008-03-06 20:32:00
BKLA
Quote
2008-03-06 20:33:00
Hervé
Quote
2008-03-07 01:02:00
Alejo
Quote
2008-03-07 01:59:00
Joilnen
Quote
2008-03-07 15:49:00
niko
Quote
2008-03-07 15:56:00
Joilnen
Quote
2008-03-07 16:10:00
nh
Quote
2008-03-16 13:35:00
Add comment:
Possible Codes
Feature | Code |
Link | [url] www.example.com [/url] |
Bold | [b]bold text[/b] |
Quote | [quote]quoted text[/quote] |
Code | [code]source code[/code] |
Emoticons
The compiler may optimize it to the same thing, but why didn't you use the pre-increment operator on bitsSet like you did with the loop iterator i?
I actually got this as a question during a Google interview: What is the fastest way to count the number of bits set in, for example, an int?