Contact Info / Websites

Entry #1

Java sucks.

2007-08-05 01:36:14 by NextGen

Just kidding. But it's really starting to frustrate me. I have to finish learning the rest of the Computer Science 2 AP curriculum by the time school starts, but I'm stuck on bitshifts (which is packet 4 of 14, and I have less than a month to go, WOOT extended summer).

So, here's the little segment of code that's pissing me off. I checked the answer with ActionScript, so I know the answers aren't wrong (some are, and that stumped me for a bit until I called my teacher =P).

Written in AS for Flashers:
x = 9, c, d;
a = new Array(4);
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
for (c = 0; c < 4; c++) {
x <<= a[c];
trace(c + " time and x is " + x);
d = x | a[c];
trace(d + " ");

and written in Java for others:
int x = 9, c, d;
int a[] = {0,1,2,3};
for (c = 0; c < 4; c++) {
x <<= a[c];
d = x | a[c];
System.out.print(d + " ");

I get the bitwise OR part. The only thing I have trouble with is the third pass of the left shift. I keep getting weird numbers. I know the output (duh), but if someone reading this could step me through the process of the loop in its entirety, that'd be super awesome. Thanks!


You must be logged in to comment on this post.