What are the rules for bitmasks? Like 0xFF vs 0xFC

I am working on a game that creates procedurally created dungeons, I found an example that uses bit masking to extract things like room number and door type.

In this example, he uses a bitmask to extract parts from an integer for each fragment. and the integer is broken as follows

0xLLSDRRET

L - is the Level Number
S - Denotes a special tile(Like Stairs)
D - is if its a door, and what type(Door, Arch, Trapped)
R - Room number
E - Flags an entrance to a room
T - Names the type of tile(Floor, Cooridor, Blocked)

In this, he uses a bitmask to get, for example, the room number, for example:

int[][] map = new int[40][40] 
int $ROOM_ID = 0x0000FF00;
System.out.println(map[x][y] & $ROOM_ID);

Now with this, if map [x] [y] was, for example, 0x00001200, the output would be 1200. This is the part of the Masks that I understand.

But in the original $ ROOM_ID ACTUALLY 0x0000FFC0, and I don’t understand what C does, because I tried different values, and I can’t understand what C does, for example

0x00001200 output-> 1200
0x00001210 output-> 1200
0x00001220 output-> 1200
0x00001230 output-> 1200
0x00001240 output-> 1240
0x00001250 output-> 1240
0x00001260 output-> 1240
0x00001270 output-> 1240
0x00001280 output-> 1280
0x00001290 output-> 1280
0x000012A0 output-> 1280
0x000012B0 output-> 1280
0x000012C0 output-> 12C0
0x000012D0 output-> 12C0
0x000012E0 output-> 12C0
0x000012F0 output-> 12C0

- , 0x0000FFC0 0x000012F0 = 12C0?

+5
3

, . ( 0) 0xFFC0 0x12F0. a bitwise and , 10. :

0xFFC0 = 1111111111100000

&

0x12F0 = 0001001011110000

0001001011100000 = 0x12F0

. (.. 8 ). , 0xFF . , , - (.. 0xF (base-16) = 1111 (base-2) = 15 (base-10)). , 8 , 4- . 0000 (0) - 1111 (F) . .

bitmasks go, . () (.. int, char ..). . , (char= , bitvector, ) 0x01, , , . 0x02, ( 0x03 ). ? , /.

0x01 = 00000001, 0x02 = 00000010, 0x03 = 00000011

, . , . ( ).

, ( ) , , , 0x01 0x01 & 0x01 = 1, 0x02 & 0x01 = 0 0x03 & 0x01 = 1 ( , )

+5

, . C 12 1100 ; F 1111 .

F 1 == 1, F 2 == 2, C F == C, 0 0 == 0.

+5

0xF - 1 : 1111. 0xF . , 0xF .

, 0x0000FFC0 & 0x000012F0 F :

0xF & 0x1 = 0x1
0xF & 0x2 = 0x2
0xC & 0xF = 0xC

:

0xFFC0 & 0x12F0 = 0x12C0

, , , 0 0. 0x0000FF00 .

0xFFC0 , . : . - System.out.println(map[x][y] & $ROOM_ID);.

0xC 1100 , , 0xC0 0xFFC0, - . , 4 . 4 , 2D, Zork-style.

, , wiki: . .

+1

All Articles