#19495 - CyberSlag5k - Tue Apr 20, 2004 10:27 pm
It pains me to ask this as it's something I know I should already know, but I don't have a very comprehensive background in hex. We briefly touched on it in my assembly course and that's about it (although I was rather good at doing hex arithmatic). Anyway, when setting the registers you OR various predefined values into them. I understand how this works. What I don't understand is how the hex values are defined.
For example, when setting REG_DISPCNT's various attributes, you define in gba.h several things.
Code: |
#define MODE_0 0x0
#define MODE_1 0x1
#define MODE_2 0x2
#define MODE_3 0x3
#define MODE_4 0x4
#define MODE_5 0x5
|
Ok, those look fine to me. I understand where the values come from. But a little later down the line in bits 4 and 5 we use
Code: |
#define backbuffer 0x10
#define H_BLANK_OAM 0x20
|
I don't understand how the values 10 and 20 correlate to bits 4 and 5. Or later on we have values of
Code: |
#define OBJ_MAP_2D 0x0
#define OBJ_MAP_1D 0x40
|
Can someone please explain?
_________________
When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
#19496 - niltsair - Tue Apr 20, 2004 10:37 pm
You need to convert those numbers to binary to understand.
Code: |
Bits 8 7 6 5 4 3 2 1
0x10 = 0 0 0 1 0 0 0 0
0x20 = 0 0 1 0 0 0 0 0
0x0F = 0 0 0 0 1 1 1 1
0xF0 = 1 1 1 1 0 0 0 0
|
What's so handy about Hex is that it is easy to figure out the binary value by looking at it. Each digit is worth 4Bits. If it's still not clear, then you'll have to look into binary values.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)
#19497 - yaustar - Tue Apr 20, 2004 10:41 pm
hex
I presume you know binary..
take a value and put it into binary
eg 150 = 10010110
divide that up to groups of 4 bits starting from the first bit
1001 0110
the values for hex go as follows
Code: |
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F |
then translate the groups of 4 into hex values
therefore:
1001 = 9
0110 = 6
so 150 to hex would be 96 or in C/C++ 0x96
in reverse
0x20 = 0010 0000
which is 32
_________________
[Blog] [Portfolio]
#19499 - ScottLininger - Tue Apr 20, 2004 11:10 pm
It easy... it's just counting.
1=1
2=2
3=3 ,etc
9=9
A=10
B=11
C=12
D=13
E=14
F=15
The right column is your "ones" column and the left column is the 16's column (just like our familiar base-10 numbers)
So the hex number "12" is 2 ones and 1 sixteen (total of 18)
The hex number "09" is 9 ones and 0 sixteens (total of 9)
The hex number "1E" is 14 ones and 1 sixteen (total of 30)
Hope that's simpler than converting to binary then back.
#19501 - CyberSlag5k - Tue Apr 20, 2004 11:22 pm
Quote: |
then translate the groups of 4 into hex values
therefore:
1001 = 9
0110 = 6
so 150 to hex would be 96 or in C/C++ 0x96
in reverse
0x20 = 0010 0000
which is 32
|
That was awsome man. Clear as a bell now. I could interchange base 10 and binary and base 10 and hex easy enough, but until that I never saw the correlation between hex and binary. That clicked right away. Thank you.
Thanks to everyone else who responded as well.[/quote]
_________________
When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
#19503 - poslundc - Tue Apr 20, 2004 11:48 pm
CyberSlag5k wrote: |
I could interchange base 10 and binary and base 10 and hex easy enough, but until that I never saw the correlation between hex and binary. |
Hexadecimal notation really comes into its own when you are concerned with anything to do with binary, because it represents binary numbers in an easy-to-read but much more compact form.
A char (or byte) is two hexadecimal digits. A short (or half-word) is four digits. An int (or word) is eight digits.
If you want to get at the 20th bit of a 32-bit variable, rather than trying to remember that 2^20 is 1,048,576 it's much easier and human-readable to use 0x00100000. And likewise, 2^20 - 1 (useful for masking with bitwise-AND) is 0x000FFFFF.
Other useful things to keep in mind: the numbers 1, 3 and 7 are the (first), (first and second), and (first and second and third) bits of a nibble turned on. (A nibble is four bits, or half a byte, or one hexadecimal digit.) So if I wanted to get just the bottom 6 bits of a variable, I would bitwise-AND it with 0x3F.
Gotta love hex...
Dan.
#19510 - CyberSlag5k - Wed Apr 21, 2004 2:25 am
Quote: |
Gotta love hex...
Dan. |
It's just a pity we humans (assuming it's safe to assume game developers are indeed human) weren't born with 8 fingers instead of 10. Then perhaps we would have devised a much more sensible counting system.
_________________
When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...
#19511 - Miked0801 - Wed Apr 21, 2004 2:29 am
What do you mean? I can count up to 1023 on my two hands ;)
#19513 - tepples - Wed Apr 21, 2004 3:37 am
In cartoon world, some races have four digits on each hand. Do those races develop octal numeration systems that are dubbed into decimal for American and European viewers?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#19514 - poslundc - Wed Apr 21, 2004 3:59 am
Of course, if you're counting with your fingers it'll be in octal anyway... so long as you don't use your thumbs.
Dan.
#19773 - sgeos - Sun Apr 25, 2004 2:51 pm
Miked0801 wrote: |
What do you mean? I can count up to 1023 on my two hands ;) |
No matter which hand I start with or which side I start from, I get embarassed once I get up to four...
CyberSlag5k wrote: |
I could interchange base 10 and binary and base 10 and hex easy enough |
Note that given any arbitrary base, n, the number 10 base n equals n. If you didn't know that already, you might want to take a second to figure out how and why that works.
-Brendan
#19781 - Miked0801 - Sun Apr 25, 2004 3:54 pm
lol:)