Android 中的移位运算符语法错误

发布于 2024-10-25 10:15:55 字数 1877 浏览 2 评论 0原文

只是想知道你是否可以在 android 中使用移位运算符,我在尝试时遇到语法错误。运算符是>> << >>>>> 。如果不支持,那是他们的 android sdk 等效项吗?

编辑:这是我正在使用的代码。我正在尝试进行每像素碰撞检测并正在尝试这一点。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2024-11-01 10:15:55

如果您要附加到字符串,除非将算术运算放在括号中,否则您会收到错误:


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

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
以往的大感动 2024-11-01 10:15:55

Android 使用的 Java 确实支持按位运算。 这是一个方便的指南

Java, which is used by Android does support bitwise operations. Here's a handy guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文