Why does this math macro work?
Ok. I just recently discovered a piece of code (in someone elses program) that will wrap an angle if it is in base 16 (I think thats the correct base). Here it is?
#define AngleWrap(a) (a & 0xFFF)
This wraps it perfectly. Anything above 4096 will be wrapped around to zero again. I use a simple 0-360 angle base in my game and was trying to repeat this math in order to get my angles to wrap around as well but couldn''t figure it out. Can anyone tell me why the above code works and if it is possible to convert it to my code as well. Thanks.
It works because the operation masks out all but the lowest 12 bits (2^12 = 4096). You can't convert it directly because there isn't an appropriate bitmask for 360 (between 2^8 = 256 and 2^9 = 512).
[edited by - spock on May 13, 2002 2:52:30 PM]
[edited by - spock on May 13, 2002 2:52:30 PM]
The and (&) operator is a bitwise function. It takes two bits (1''s or 0''s) and returns a new value based on the parameters:
In this case he''s using it as a mask (filter), causing only those bits that are also present in 0xFFF to remain in the result. Since 4096 is a power of 2, he can use the highest value in your range (4096-1) 4095=0xFFF, which in binary is 111111111111 to mask only those bits. Any bits over that will be elimated, thus keeping it within that range.
(If you want more information you could try a search on Google for "Binary Math")
AND Table0 & 0 = 01 & 0 = 00 & 1 = 01 & 1 = 1
In this case he''s using it as a mask (filter), causing only those bits that are also present in 0xFFF to remain in the result. Since 4096 is a power of 2, he can use the highest value in your range (4096-1) 4095=0xFFF, which in binary is 111111111111 to mask only those bits. Any bits over that will be elimated, thus keeping it within that range.
(If you want more information you could try a search on Google for "Binary Math")
This is pretty dumb, and you probably know about it, but no one mentioned it. You can use the remainder to wrap around integer values:
x % 360
If it''s not an integer, then use
x -= ((int)(x / 360)) * 360
Cédric
x % 360
If it''s not an integer, then use
x -= ((int)(x / 360)) * 360
Cédric
#define AngleWrap(a) (a & 0xFFF)
I was under the impression that that you had to "wrap" parameters ALWAYS in macros. ie, the above should look like:
#define AngleWrap(a) ((a) & 0xFFF)
argh...it''s reasons like this macros should be completely eliminated in non-C progs. __inline or __forceinline is route of least headache, plus, you can step through the code.
Just my 2 cents
I was under the impression that that you had to "wrap" parameters ALWAYS in macros. ie, the above should look like:
#define AngleWrap(a) ((a) & 0xFFF)
argh...it''s reasons like this macros should be completely eliminated in non-C progs. __inline or __forceinline is route of least headache, plus, you can step through the code.
Just my 2 cents

People fear what they don''t understand, hate what they can''t conquer!
quote:
Original post by cedricl
This is pretty dumb, and you probably know about it, but no one mentioned it. You can use the remainder to wrap around integer values:
x % 360
If it''s not an integer, then use
x -= ((int)(x / 360)) * 360
Cédric
You can do it that way, however the modulus operation is much slower than the and operator (At least 3 times slower on the most recent PIII, Athlon machines, as much as 50 times slower on really old 286 machines).
No, you don''t always have to wrap parts of a macro in parintheses, and
compiles the same as
because it is literally pasted into the code where the macro is used by the preprocessor...
#define AngleWrap(a) (a & 0xFFF)
compiles the same as
#define AngleWrap(a) ((a) & 0xFFF)
because it is literally pasted into the code where the macro is used by the preprocessor...
[email=erydo@gdnmail.net" style="color: #ff0000; text-decoration:none; cursor:help;](o= erydo =o)[/email]
So does anyone know the exact conditions where macro parameters must be enclosed by parantheses?
Don''t get me wrong, I still use macros, but for simple cases only.
Don''t get me wrong, I still use macros, but for simple cases only.

People fear what they don''t understand, hate what they can''t conquer!
quote:
Original post by cliffhawkens
No, you don''t always have to wrap parts of a macro in parintheses, and
compiles the same...
because it is literally pasted into the code where the macro is used by the preprocessor...
False. Just look at this:
#define AngleWrap(b) (b % 360)...x = AngleWrap(z + 30);
Without parenthases, it becomes
x = z + 30 % 360;
Which is equivalent to
x = z + (30 % 360);
See my point?
Anyway, it really shouldn''t matter; just use inlined functions.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement