11 March 2016 - Forum Rules

Main Menu

how Flag Codes work on rom hacking

Started by jin299, October 29, 2012, 05:02:48 PM

Previous topic - Next topic


Hello, does any know, how these type of coding, is used in rom hacking?


Are you talking about bitflags?

Bitflags set one bit within a byte.  A byte contains 8 bits, and that's usually the basic method of storage in computers today.  Bytes can also describe numbers ranging from 0-255 through the magic of hexadecimal, and this works because each bit is a different power of 2.

Hexadecimal is a convenient way to encode several types of data in a simpler way.  For our purposes it can describe both 8 bits and 1 byte.  So, if you have eight bits:
-you can write that as one byte in hex:
The only complication is that you need to count to 16, so we use A for 10, B for 11, C for 12, all the way to F.

Since binary is base 2, each bit is a power of two. 00000010 is 2, 00000100 is 4, 00001000 is 8, and so forth.  These are added together to make integer numbers.  Here's a table:

00 0
01 1
02 2
04 4
08 8
10 16
20 32
40 64
80 128
FF 255

Sometimes though they aren't necessarily describing numbers but are used as flags to tell if something is on or off.  This is especially true of options; look at any save file and the options are probably going to be stored as bitflags since most are just on/off.

Funny thing is that computers usually consider the smallest piece of data they want to work with to be a byte.  So, to set or toggle a flag you'll have to OR or XOR it with it's numerical value.  To set flag 0x04, you'd OR it with 4.  You could also do something funny like OR (1<<2) if you want to complicate things.

It's incredibly useful and you'll see it all the time.  You can save a lot of space (though lose a tiny bit of CPU) by encoding things with just bits.  Likewise, because programs use them all the time you need to be familiar with it when hacking--especially with options and registers. 
As an example, the N64 uses bitflags for all its status registers.  If you want to change the graphics mode, know when data has finished loading, or check for errors, you need to look at certain bitflags in those registers. 


Than you, that was rather informative.