I don't know whether you've noticed or not. But lately, I just haven't
been myself.
I've had all kinds of little snippets of code -- complete with
parenthesis, semi colons and >>>> (whatever they're
called) in my blog. I understand none of it. I stress out about whether
i did the copy; paste right because the indentation and the font of the
code and all that have to be just right so that it makes sense. And
every time one of you guys responds to me, i have to hit the forward
button and bother one of my techno celeb friends to get them to explain
to me what you're saying.
It was all part of a stupid attempt to impress you. And frankly, it's
exhausted me.
You're getting the explanation from the
Ultimate
Puzzler Challenge. And
then we're laying off the code for a while.
Because the whole shtick about
MaryMary
understanding the techno speak
is a complete farce.
We're going back to the world according to
MaryMaryQuiteContary. The
world where you get helpful household hints about how to
clean
out your
car with a leaf blower. The world where you find out who was
singing
in the shower with me in Shanghai. The world where I use this
blog to explicitly and without apology
market to you (and in doing so
whisper
stupid
nothings into your ear).
So to quote my new role model,
Gianna
Angelopoulos–Daskalaki
Welcome Home!
But before we come full circle, we've got to finish what we started.
Here's the explanation to the
Ultimate
Puzzler Challenge (and this is
straight from a personal email from Click and Hack which was signed
"Kisses"):
The loop was:
while (k != 0)
k >>>= 1;
This one was tough. For the shift to be
legal, k has to be an integral type, and it looks like
the unsigned right shift operator will produce a result closer to zero
on each iteration. There is, however, one small flaw in this logic. The
>>>= operator is an assignment operator, and
assignment operators have a hidden cast in them. The cast can be a narrowing
cast, which throws away information. Suppose you use this
declaration:
short k = -1;
Here's what happens. First the value of
k (0xFFFF)
is promoted to an int; all arithmetic operations
do that
if their operands are of type short, byte,
or
char. The promotion involves sign extension, so
the
resulting value is 0xFFFFFFFF. This value is shifted right one bit
without
sign extension, which yields 0x7FFFFFFF. Now here's the kicker: when
this
value is stored back into k, the implicit narrowing cast
that I
mentioned earlier chops off the high order 16 bits, leaving 0xFFFF, and
we're
back where we started.
The moral of this sad story is that assignment operators are dangerous
when you use them on short, byte and char
values? You end up doing mixed mode arithmetic, which is always
a tricky business. Worse, you end up doing a narrowing cast (which
throws away information) even though it doesn't show up in the code.
See, entertaining you is more my style. I'll still ask Click and
Hack to give us puzzlers here and there. They can educate you. We'll
still do Friday Free Stuff. It's just that you won't always have to be
smart to win. That way, people like
me could win. (Except that I'm a Sun employee and Sun employees are
ineligible for the prize; plus the point of Friday Free Stuff is to
give away stuff that I don't want; so me winning would kind of defeat
the purpose.)
So that's the story, people.
I feel much better now.
:-)
mary