 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
Seven bits? Seven???
Has anyone considered what happens if a bit has to be large enough to allow values of 0, 1 and FILE_NOT_FOUND?
Anyway, here's my two bits, for what it's worth: 11
Admin
((x % 2) == 0) implies that you are directly checking to see whether the value of x is evenly divisible by 2 (is it even or odd?)
((x % 1) == 0) implies that you are checking to see whether the low bit of the binary value is set to 1. By the way, if the bit is set/not set then the value is even or odd.
The first version directly shows what you intend to test. the second version is a little more indirect...
Admin
Whenever I'm trying to make alternatingrow styles, I use something like:
string NextRowStyle() { static bool e = true; e = !e; return e ? "even" : "odd"; }
Admin
Besides, instead of
it is better to </realprogrammer>Admin
Err, me thinks you missed the 2^32 bits part.
Admin
string NextRowStyle() { static bool e = true; e = !e; return e ? "even" : "odd"; }
Beware using static!! Can result in some rather subtle and mysterious errors in a multithreaded program.
Admin
string NextRowStyle(ref bool IsEven) { IsEven = !IsEven; return IsEven ? "even" : "odd"; }
Admin
Similarly no static local variables or refs, so string NextRowStyle(ref bool IsEven)
would have to become String NextRowStyle(boolean[] IsEven) { IsEven[0] = !IsEven[0]; return IsEven[0] ? "even" : "odd"; }
Admin
Admin
I'm pretty sure said client isn't actually printing the data (I sincerely hope not!). Maybe they intended to analyze it in Excel (not bloodly likely given its row limit) or their own database (possible, but a pointless waste of bandwidth since we've spent years on this and could just be providing the analytics they want).
But hey, boys will be boys. If they like playing with raw data then who am I to say no. I just hope their Real Programâ„˘ accepts (SQL) bigints!
Admin
I hope that, after Eric made his change, he checked the REST of the system to see if his reversal of the program's meaning of "even" and "odd" (which, granted, was wrong to begin with) had any impact on behaviour.
Too often, in systems like this, a small bug like that becomes an integral part of the structure that holds the whole thing up. Like, if the system needs for "0" to be "odd" in order to jumpstart some counter that gets the whole thing going, or some such.
Admin
Also, what if (index == 0)?
Admin
You mean Java has a class for handling 'arbitrarily large' values, but puts a 2^(2^32)1 upper bound on it? Pah!
I wanted to use big numbers!
How many decimal digits has 2^(2^32)1 got, anyway?
Admin
About 1292914005.11488.
Is that enough?
It's more than 640k.
Or did you mean 2^(2^321)?
Admin
Alex, you have a typo in the WTF: too many closing parentheses.
Admin
Perhaps java's BigInteger mod method only takes one type of parameter ... oh say, another BigInteger?
Granted, the use of BigInteger in this situation is quite unnecessary but given that it is used, do you have another way to divide a BigInteger?
Admin
Probably; I think you'd still be browsing that same page. If you hate it now, you'll still hate it then.
Admin
Excuse me? How is x%2 any more/less readable than x&1?
By your argument x%2 implies you are directly checking to see if x is evenly divisible by 2. By the way, if it is evenly divisible by 2 it is even. x&1 imples you are checked to see whether the number is even because the last bit is set/unset.
The second version directly shows what you intend to test. The first version is a little more indirect...
Admin
This reminds me of a former coworker. My friends an I called him Mr Int64. He insisted on using int64's, and claims he has done so since the early 90s.
Int64 has some uses. For example if you are counting the grains of sand on the beach. And if you are on an 64 bit machine, its probably fine and dandy to use Int64. But on a 32 bit machine, for day to day processing, the word size of the machine is just fine.
We first got into the argument, when I noticed he was using Int64 for pixel location on the monitor. Apparently he didn't realize that a montior would have to be like a mile(or some insane size) wide before 32 bits wasn't large enough.
But one day he turned to me, with a huge smile. "I know how to overflow int32." I had to know what he thought was too big, so I asked. "Well what if you wanted to store all the possible colors?" "Well", I responded... "we use 32 bit color, so..." "Hmm yeah, but what if you wanted all the possible transparencies as well" "Again, its 32 bit color..."
Before he left, he came up with one more reason. Our original prototype was using an int for an ID. We would probably only have 4 or 5 objects, maybe a dozen, with a max of a hundred or so... Ahhh but perhaps we should use Int64, because if we implement this one feature we might have 10 million!
Admin
Why not use BigInteger.ONE + BigInteger.ONE for your purposes?
Admin
I've seen someone use a calculator to work out 1 * 1
Admin
Admin
I have seen A LOT of similar stuff at my university. How about a c++ class (For a game none the less) containing a quadtree of which 2/3rds of the code were simply crud that did nothing. And the rest of the code were not too good. How about accessing arrays with negative numbers? AND then using the return values of that for the functionality! Aaargh!!!
Or the Object class that had the getRotationMatrix() method, that returned a matrix from inside the class that was named rotation. But it was only ever initialized, not used for the rotation that that class handled. So when you called that metod, all you ever could get was an identity matrix. AND the math library used had Matrix.Identity(). So getting identity matrices was easy. Now, when I spotted that method, I fixed it, because I needed such a funktion. With all hell breaking lose because their game, built on this engine (We were making a game as well with it) actually used that method. WTF? If they need an identity matrix, use the math lib.
Now, I could keep going for, oh, say, half an hour to an hour with the atrocities that engine contained. But I wont. ;)
Admin
The real WTF is that they did not use an iterative method, which would be approximately seven times faster than this one.
Admin
Admin
I do not know if this works in Java the same (but I think it does), but in C/C++ I (would) use:
((index & 1) ? "odd" : "even")
Bit operations are faster then a moduloDIVISION! (And because it's a index, I can assume it's always positive, can't I?)
Admin
Not true when x is negative.
Admin
The real reason of jealousy of computer scientists is they don't know how to do math properly. Rare universities teach proper math to computer science students :(
Admin
I actually think this produces the intended result. index == 0 > 1st Row (odd) index == 1 > 2nd Row (even)
Remember that the rest of the world starts counting at 1, not zero.
Admin
BigInteger is nice but its abominably slow for what it does...
A friend of mine was futzing around with Java and coded a basic RSA algorithm in Java. It was taking minutes to encrypt even the smallest message.
So I showed him the light of GMP Bignum.
It now goes over a million times faster.
Admin
Allocate an array, then assign the mem address of the middle of the array to a pointer that matches the array type, and proceed to treat the pointer like an array.
(After all, it's C)
Something like
Admin
If an index into an html table needs more than 32 bits, I dont think how you get odd or even really matters, because there are greater problems....
Admin
Admin
Yeah. A Real Programmer would have implemented it in LISP, and used a cons cell for each bit.
(A Really Real Programmer would have implemented it in a single line of APL. No point in wasting time with inferior tools.)
 Michael Wojcik
Admin
They are both indirect. Why don't you just ask the number if is even or odd?
Admin
I'm pretty sure it is
5%2=1 5= 1[...]111011 5&1=1
Rich
Admin
char opts[]="even\0\0\0\0odd\0" return opts+((index&1)<<3)
I win!
(Cept this is way late).
Rich
Admin
No, the JVM cannot do that optimisation since posisble values of x%2 are 1, 0, 1, while x&1 can only be 1, 0. How the hell would the JVM know that x is always positive? Last time I checked Java had no unsigned integers.
In other languages where % is "real" mathematical modulo the optimisation would be possible.
Admin
$ sbcl
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Mind ya, this would look the same on some LISP 30 years ago.
There's your progress.Cheers, Kuba
Admin
Umm, you meant like this?
Perfectly valid C (and C++!), and this is production code.
Cheers, Kuba
Admin
This scares me:
My class just finished a makeaBigInt assignment a couple weeks ago... figure about now you've got many of them graded... and I know my teacher hates most of us...
I wonder...
(Take note that my name is not Eric)
Admin
A mathsprofessor friend of mine commented the other day after I got caught by a rounding error, "Why is it that programmers don't use rationals?"
I told her, "Because we don't know what they are."
After several hours of instruction (it felt like being back at school), I once again understood GDC and LCM and had written a library. It's amazing how much easier mathematical programs have become since then.
Cliff.
Admin
Admin
Ok, pedantic killjoy necromancer at work here, but it's actually a fairly reasonable function to come up with given the circumstances:
Java learned by reading docs as required on the fly not from a boring ground up textbook or course. Hence, the % (mod) operator has been completely missed (or entirely forgotten in the mists of time). Need to do a Mod, check javadocs, only 1 Mod method exists! It's on a BigInteger, oh man! Here we go, converting that int to a BigInt so that I can access the damn Mod method. Oh look, it only takes another BigInt! I can't even pass 2 in. Oh those BigInt constructors are pretty damn obtuse. Ah, a string based constructor that will just parse the string, guess I'll have to do that. I'm not creating a new BigInt("0") object to see if it's zero or not, once was enough, I'll just return the result as a string and check that. See I'll have an additional static "0" string which would get parsed every time anyway, this would probably actually be faster. The fact that it returns "odd" for even numbers would be completely ignored as all that's really needed is alternating classes, which is what this provides.
So the whole debacle comes from not knowing about the builtin % operator. Int doesn't have a "mod" method. BigInt does, making it orders of magnitude more discoverable.
Perhaps at least some of the problem can be put down to documentation being method heavy, whereas builtin operators need to be explicitly searched for in a different place entirely. Perhaps if the int documentation included "operator%" or something, then at least you'd only end up with return value.operator%(2) == 0?"odd":"even"; (sic)
Umm, should I be scared that I can reproduce a logical flow to generate this method so easily? Should I be more scared that I care enough to post this after this thread died so long ago?
Admin
Does it really have to do with 32/64 etc. bits ? just take LSB of the number and if it is 1 then its an even number and if it is 0 then it is odd. Or take the number on the unit's place and divide it by 2 if remainder is 0 it is even if remainder is odd the number is even !