#6062 - wiz - Fri May 16, 2003 12:57 pm
Just a quick message, my 1st message on here with a 1st question.
Having recently read through a few tutorials I have been playing in mode 4.
I would like to know how to extract the high and low values from a u16.
if anyone knows of a good resource for learning binary stuff then please let me see the link :)
Thanks very kindly
#6065 - gb_feedback - Fri May 16, 2003 1:51 pm
Quote: |
I would like to know how to extract the high and low values from a u16. |
Something like:
u8 lowValue = (u8) (wordValue & 0xff);
u8 highValue = (u8) ((wordValue >> 8) & 0xff)
You'd probably need the casts to avoid a warning, if lowValue and highValue are actually u8's.
_________________
http://www.bookreader.co.uk/
#6100 - wiz - Sat May 17, 2003 3:47 am
Great thanks for that, worked fine, now that leads to my next question.
what exactly does "& 0xff" do / mean?
I'll see if I can find some help on working with binary but if anyone does know of a great resource then please let me know :D
Thank you very much for the reply! :)
#6105 - DracoX5 - Sat May 17, 2003 5:17 am
The & operator ANDs numbers together. In other words, it compares the bits of two numbers and for every bit that is 1 in both numbers, there will be a 1 in the new number. If either one of the two bits is a 0 (or both are), it'll give a 0 for that bit.
Example:
001011010 = 0x5A
&
001101000 = 0x68
_________ ____
001001000 = 0x48
0x5A & 0x68 = 0x48
All "& 0xff" does is preserve the lower byte and get rid of the higher byte.
Other operators you should know are |, ^, ~, <<, and >>. You should be able to find these in any respectable C tutorial. I learned from books, so I don't have any links for you. Sorry! Just try Google, and you should find some, though.
#6108 - wiz - Sat May 17, 2003 6:05 am
fantastic!
Makes perfect sense now, I'll be sure to look at some c tutorials in that area tonight (I know how to make classes, functions etc so Im not that bad. I have just never had the need to do all the low level stuff before getting in to the gba)
thanks again, you guys are great :)
#6109 - niltsair - Sat May 17, 2003 6:32 am
Quickly here's the description :
val1 & val2 (AND) Keep only the bit that are in both value
val1 | val2 (OR) Keep the bits that are in either value
val1 ^ val2 (XOR) Keep the bits that are either in one value or the other. If a bit is present in both, then it'S equl to 0.
~val (COMPLEMENT) Invert the value of each bit.
val << X (LSHIFT) Shift every bit to the left by X
val >> X (RSHIT) Shift every bit to the right by X
(OR)
0011010
1001001
-----------
1011011
(XOR)
0011010
1001001
-----------
1010011
(COMPLEMENT)
0011010
-----------
1100101
(LSHIFT by 3)
0011010
-----------
1010000
(RSHIFT by 3)
0011010
-----------
0000011
#6141 - wiz - Sun May 18, 2003 5:09 am
Hi again,
I thought I understood the reply about the "& 0xff" part..
having thought about it, this wouldnt do anything to a byte?
Sorry if I missed something, but I guess I dont understand why it is included within:
u8 lowValue = (u8) (wordValue & 0xff);
u8 highValue = (u8) ((wordValue >> 8) & 0xff)
Why not just
u8 lowValue = (u8) (wordValue);
u8 highValue = (u8) ((wordValue >> 8))
?
Thanks again
#6144 - niltsair - Sun May 18, 2003 5:41 am
Ok, a Word value is 16bits (2Byte)
Can want to retrieve the HighWord and LowWord, independantly.
So if you have this value :
1111 0000 1010 1100
1111 0000 (High Value)
1010 1100 (Low Value)
Now, to retrieve the LowWord :
1111 0000 1010 1100 ==>(wordValue & 0xFF);
0000 0000 1111 1111
-------------------------
0000 0000 1010 1100 ==>( (u8) ) (Keeps first 8bits)
-------------------------
Final Result : 1010 1100
Retrieve the HighWord :
1111 0000 1010 1100 ==>(wordValue >> 8);
-------------------------
xxxx xxxx 1111 0000 ==> ( & 0xFF )
0000 0000 1111 1111
-------------------------
0000 0000 1111 0000 ==> ( (u8) ) (Keeps first 8 bits)
-------------------------
Final Result : 1111 0000
The x's are because the value is unknown.
#6145 - wiz - Sun May 18, 2003 7:23 am
hi again :)
I still think there is no need for the "& 0xFF"
lets take this 16bit value again:
1111 0000 1010 1100
1111 0000 (High)
1010 1100 (Low)
u8 low = (u8)wordvalue;
since its only taking 8bits from it wouldnt it just take whats needed?
1111 0000 [1010 1100]
then for the high part
wordvalue >> 8 would do this:
xxxx xxxx 1111 0000
and again only taking 8bits..
u8 high = (u8)wordvalue>>8;
xxxx xxxx [1111 0000]
is there definately the need to & 0xFF the word value?
sorry to drag this out, I really do want to fully understand though
and thank you again :)
#6147 - gb_feedback - Sun May 18, 2003 8:19 am
I think you've got a point.
When I posted the original I didn't try it, but it is coded 'defensively' because I am fed up with getting unnecessary error messages. The problems occur when you daily use different C++ compilers as I have to, and there are lines of code in some compilers which genuinely do something not required if you don't take some of these steps. And as it's so illogical it takes a devil of a time to find the problem. So I usually go to some lengths to make sure the compiler understands.
However, if you take out the mask and get the same results - that's fine too!
_________________
http://www.bookreader.co.uk/
#6151 - wiz - Sun May 18, 2003 12:09 pm
Great!
I understand why it is used :) But I doubted myself when thinking about it. If it wasnt for you adding that line in to code I wouldnt have had such a learning experience over it all and I feel Im definately getting to grips with it all so much more now.
Thank you all :)