- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I just cannot unsee it!
Admin
Duh. Not only is counting done from 1 to 4, it also starts on the wrong end of the nibble. Not bad. :)
Admin
Oh sweet Mother of God!
Admin
My eyes!
Admin
Well that would be my implementation. Probably a better /shorter way somewhere.
Admin
This guy was obviously paid by the line. Laugh all you want, but he's now sipping martinis on his private 100m yacht.
Admin
Dear sir, we are interested in your implementation of this function. We find it to be sufficiently complex for use in our enterprise software, as we find that all functions under 1000 lines are inadequate and poor. Please expand on your comments somewhat, and we would be willing to pay the going market rate for this product ($200/LOC).
Yours sincerely,
PHB
Admin
hello freind
i am try ot hex some String for collage. but you Jaava is not work. ther is nto complier erorr.
how to mak wrok?
emial of list plz?
Admin
What about Integer.toHexString() ?
Admin
Well, I dunno much about java, but C... *("0123456789ABCDEF" + ((bit1 << 3) | (bit2 << 2) | (bit3 << 1) | bit4)) should work...
But at least their version is easier to extend if hexadecimal suddenly gets a 17th character...
Admin
No fewer than!
Admin
So THAT'S why my Vista runs so slow...
Admin
Admin
I don't know much C but I love this solution :)
Admin
Admin
A slightly better version would be one that works with Streams and TextWriters.
Admin
In Java it can indeed be done in 1 line:
Admin
We had a related wtf here a couple of weeks ago. On my current project, we use BigInteger to do this conversion:
new BigInteger(1, data).toString(16);
which should be fine. Except that our byte-arrays actually represent sha1 hashcodes. We spent a long time scratching our hands over strange checksum mismatches in the code, until it finally came to our attention that the problem only arose when the checksum had leading 0's .
Admin
Wow, I've written it more concisely in XSL (version 1)
Admin
Admin
Python:
Admin
In C:
what about a good old sprintf?
Admin
In C, such should always be written as (bit18+bit24+bit3*2+bit4)["0123456789ABCDEF"]
Admin
That's just fugly.
Admin
Linguists say no! Or actually, they say "prescriptivist poppycock", but it's much the same thing.
Admin
Admin
Pah, who says that the numbers of bits in a byte will be an integer value after the reboot. Put everything in strings and decode it as required.
Admin
Admin
In Java, a byte does have 8 bits.
Admin
Admin
Admin
That's so obviously wrong! The arrays are never used!
A proper implementation should be: public static final object[] F_BITS = new object[]{true, true, true, true, F, F_LOWER};
and then they can do the check by simply doing: if (bit1 == F_BITS[FIRST_BIT_OFFSET] && bit2 == F_BITS[SECOND_BIT_OFFSET] && bit3 == F_BITS[THIRD_BIT_OFFSET] && bit4 == F_BITS[FOURTH_BIT_OFFSET]) return F_BITS[LOWERCASE_OFFSET];
(They need to add constants for LOWERCASE_OFFSET and UPPERCASE_OFFSET and use the same number constant for both from 0 to 9).
for each letter in the array. There, much better.
Admin
or hex = sprintf(i, '%x'); // not sure on the java syntax
Admin
Shift and OR operations are faster than multiplication. I'd be curious to compare the assembly output from an optimizing compiler for each and see which is more efficient.
Admin
Yakka foob mog, grub pubbawup zink watoom gazork. Chumble spuzz.
And if you don't like those words, you're being prescriptivist. I think they're perfectly valid.
Admin
Perl code examples plz? ;-)
Admin
a (good) optimizing compiler will most definitely transform the "bit1*8" to "bit1<<3" as long as bit1 .. bitn are integer/byte/... types
btw. see also this (i love this example - code like that has driven me nuts on "reengineering" ) http://www.nynaeve.net/?p=115
Admin
Indeed, quite cromulent.
Admin
Admin
They use not only too many lines to do a simple things that could be done in one line, but they have way too many comments, also!
Admin
?
Admin
Admin
Admin
One day, after a code review by one of our 'senior' developers this little beauty showed up...
#define NUMBER_OF_BITS_IN_BYTE 8
The justification was... that the byte might change.... and no 'magic' numbers should appear in the code...
Admin
Not necessarily, depends on the platform. Some platforms only have 1-bit shift operations, and on those platforms, bit1<<3 would need a loop to be implemented, which may be slower than a multiplication.
Anyway, good optimizers always decide, on a case-per-case basis, which one is faster, so programmers should just stick to what is more readable (do you actually want to multiply values, or just shift bits?).
Never try to do the optimizer's work manually!
Admin
Actually I can understand this. Not because the byte might change but rather to document the code. Why make the next guy who has to maintain your code have to figure out what the number 8 represents when you can make it glaringly obvious this way?
Admin
I'm sure the code making use of this depends on a certain bug in this "algorithm", so you won't be able to exchange it.
Admin
Admin
From: Management To: Code Monkey Date: Friday, August 29, 2008 04:15 pm Subject: Labor Day Holiday
Mr. Mankey,
I have received your request for time off on Monday. This will be no problem as long as you finish the hexadecimal conversion method assigned to you earlier this month. It is looking good so far, but the client has requested a small piece of additional functionality. Please upgrade your method to allow balanced ternary input and provide the option for base 42 output. This should be an easy fix for an experienced programmer. Remember, the new method must be released to production by the end of the day.
Regards,
P. H. Boss
Admin
This one is cleaner: (bit18 + bit24 + bit32 + bit4 + "0123456789ABCDEF")