2011-03-19 28 views
0

只是想知道你是否可以在android中使用移位運算符我在嘗試時遇到語法錯誤。運營商是>> < < >>>。如果它不支持它是他們的android sdk等價物?移動運算符在android中的語法錯誤

編輯:這裏是我使用的代碼。我試圖做一個像素碰撞檢測,並嘗試了這一點。

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"); } } }
+2

是的,可以。請發佈不起作用的代碼,否則我們無法真正幫助。 – fredley 2011-03-19 23:54:42

+0

我用我的代碼編輯了原始問題。對不起 – pengume 2011-03-20 00:32:51

回答

1

Android中使用的Java確實支持按位運算。 Here's a handy guide

+0

謝謝我發佈我的代碼上面。不知道爲什麼它不工作,也許你可以幫忙? – pengume 2011-03-20 01:26:40

2

如果你要追加到一個字符串,除非你把算術運算括號,你會得到一個錯誤:


[email protected]:/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)); 
} 
} 
[email protected]:/tmp$ java test 
21, 84, 21 
相關問題