What is the meaning of this line?

I am watching an exemplary game project. Can you explain these lines:

public static final int GAMEPAD_UP = 0x0040; 

What is 0x0040; ?

This is the full code:

 package com.androidemu; public class Emulator { public static final int VIDEO_W = 240; public static final int VIDEO_H = 160; public static final int GAMEPAD_UP = 0x0040; public static final int GAMEPAD_DOWN = 0x0080; public static final int GAMEPAD_LEFT = 0x0020; public static final int GAMEPAD_RIGHT = 0x0010; public static final int GAMEPAD_A = 0x0001; public static final int GAMEPAD_B = 0x0002; public static final int GAMEPAD_SELECT = 0x0004; public static final int GAMEPAD_START = 0x0008; public static final int GAMEPAD_TL = 0x0200; public static final int GAMEPAD_TR = 0x0100; public static final int GAMEPAD_A_TURBO = (GAMEPAD_A << 16); public static final int GAMEPAD_B_TURBO = (GAMEPAD_B << 16); public static final int GAMEPAD_UP_LEFT = (GAMEPAD_UP | GAMEPAD_LEFT); public static final int GAMEPAD_UP_RIGHT = (GAMEPAD_UP | GAMEPAD_RIGHT); public static final int GAMEPAD_DOWN_LEFT = (GAMEPAD_DOWN | GAMEPAD_LEFT); public static final int GAMEPAD_DOWN_RIGHT = (GAMEPAD_DOWN | GAMEPAD_RIGHT); public native void setRenderSurface(EmulatorView surface, int width, int height); public native void setKeyStates(int states); public native void setOption(String name, String value); public native boolean initialize(String libdir, String datadir); public native void cleanUp(); public native void reset(); public native void power(); public native boolean loadBIOS(String file); public native boolean loadROM(String file); public native void unloadROM(); public native void pause(); public native void resume(); public native void run(); public native boolean saveState(String file); public native boolean loadState(String file); public void setOption(String name, boolean value) { setOption(name, value ? "true" : "false"); } static { System.loadLibrary("gba"); } } 

What is the purpose of these values?

+6
java
source share
4 answers

This is a value that has exactly one bit:

 00000000000000000000000001000000 

That way you can also have, say, GAMEPAD_RIGHT=0x0010 , and you can OR evaluate and test any of them regardless of AND'ing. This is very common in games, especially on mobile devices.

The purpose of this coding is to optimize the space and the ability to test various cases on one line.

Using one bit for, say, all possible gamepad keys, you can represent on one int (even one byte in the old 8-bit days of the console, where the console has very few keys) the state of each key (it is either 'on' or 'off ').

UP + RIGHT, while holding down the key:

 00000000000000000000000001000000 OR 00000000000000000000000000010000 = 00000000000000000000000001010000 
+11
source share
  public static final int GAMEPAD_UP = 0x0040; public static void main(String args[]){ System.out.print(GAMEPAD_UP); } Output: 64 

0x0040 is a hexadecimal representation (base 16) 64 (base 10).

+1
source share

This is a definition of an integer constant with a hex value of 0x0040 .

0
source share

The concept is that the string assigns the ASCII code to the variable GAMEPAD_UP. This means that after the keyup event occurs, you can find the source of this event inside the eventHandler (source) method.

And, according to the source, you will make changes to the gameplay .;)

-3
source share

All Articles