Damn, you're right about the byte size. Replace 8 with CHAR_BITS.
kdean
@kdean
Best posts made by kdean
Latest posts made by kdean
-
RE: Definitely a WTF
-
RE: Definitely a WTF
No, you can't replace the for-loop with a "while (theWord > 0)". If theWord = 1, the reversal would stop after one iteration.
-
RE: Erm...
No matter what the integer size, this would work a whole lot better:
<FONT face="Courier New"><FONT size=+0>unsigned long reverse(unsigned long theWord)
{
unsigned long result = 0;</FONT></FONT><FONT face="Courier New"><FONT size=+0> int i;</FONT> </FONT>
<FONT size=+0> for (i = 0; i < sizeof(theWord) * 8; ++i) {
result <<= 1;
result |= theWord & 0x01;
</FONT><FONT size=+0>
theWord >>= 1;</FONT><FONT size=+0>
}</FONT><FONT size=+0> return result;
<FONT face="Courier New">}</FONT></FONT> -
RE: Definitely a WTF
Whoops, didn't mean to create a new thread. Too new to the forums...
-
Definitely a WTF
No matter what the integer size, this would work a whole lot better:
<FONT face="Courier New"><FONT size=2>unsigned long reverse(unsigned long theWord)
{
unsigned long result = 0;</FONT></FONT><FONT face="Courier New"><FONT size=2> int i;</FONT> </FONT>
<FONT size=2> for (i = 0; i < sizeof(theWord) * 8; ++i) {
result <<= 1;
result |= theWord & 0x01;
</FONT><FONT size=2>
theWord >>= 1;</FONT><FONT size=2>
}</FONT><FONT size=2> return result;
<FONT face="Courier New">}</FONT></FONT>