Reputation: 1
Was just wondering if you could use the shift operator in android I am getting a syntax error when trying it. the operator is >> << >>> . If it doesn't support it is their an android sdk equivalent?
EDIT: here is the code i am using. I am trying to do a per pixel collision detection and was trying this out.
public void getBitmapData(Bitmap bitmap1, Bitmap bitmap2){
int[] bitmap1Pixels;
int[] bitmap2Pixels;
int bitmap1Height = bitmap1.getHeight();
int bitmap1Width = bitmap1.getWidth();
int bitmap2Height = bitmap1.getHeight();
int bitmap2Width = bitmap1.getWidth();
bitmap1Pixels = new int[bitmap1Height * bitmap1Width];
bitmap2Pixels = new int[bitmap2Height * bitmap2Width];
bitmap1.getPixels(bitmap1Pixels, 0, bitmap1Width, 1, 1, bitmap1Width - 1, bitmap1Height - 1);
bitmap2.getPixels(bitmap2Pixels, 0, bitmap2Width, 1, 1, bitmap2Width - 1, bitmap2Height - 1);
// Find the first line where the two sprites might overlap
int linePlayer, lineEnemy;
if (ninja.getY() <= enemy.getY()) {
linePlayer = enemy.getY() - ninja.getY();
lineEnemy = 0;
} else {
linePlayer = 0;
lineEnemy = ninja.getY() - enemy.getY();
}
int line = Math.max(linePlayer, lineEnemy);
// Get the shift between the two
int x = ninja.getX() - enemy.getX();
int maxLines = Math.max(bitmap1Height, bitmap2Height);
for (; line <= maxLines; line ++) {
// if width > 32, then you need a second loop here
long playerMask = bitmap1Pixels[linePlayer];
long enemyMask = bitmap2Pixels[lineEnemy];
// Reproduce the shift between the two sprites
if (x < 0) playerMask << (-x);
else enemyMask << x;
// If the two masks have common bits, binary AND will return != 0
if ((playerMask & enemyMask) != 0) {
// Contact!
Log.d("pixel collsion","we have pixel on pixel");
}
}
}
Upvotes: 0
Views: 1874
Reputation: 38462
If you're appending to a string you'll get an error unless you put the arithmetic operations in parentheses:
jcomeau@intrepid:/tmp$ cat test.java
public class test {
public static void main(String args[]) {
int test = 42;
System.out.println("" + (test >> 1) + ", " + (test << 1) + ", " + (test >>> 1));
}
}
jcomeau@intrepid:/tmp$ java test
21, 84, 21
Upvotes: 2
Reputation: 33911
Java, which is used by Android does support bitwise operations. Here's a handy guide.
Upvotes: 1