# Macros issues

This topic is 5388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey I have the following code but when I try to use it, it just doesn't seem to work. Could anyone tell me what wrong with it ?

#define INT2FIX(x) (x << 16)
#define FIX2INT(x) (x >> 16)

int main()
{
...
c = INT2FIX(a);
d = FIX2INT(c)
}


I seem to get c = 0 and d = 0 if I pass a = 1. While I should get 65536 and 1. Any Ideas as to where I could be going wrong. Or I can't use macros like this ? Thanks

##### Share on other sites
What's the type of c? If it's too small, it may be overflowing or rolling over. Make sure it's a 32-bit type, because your value needs 17 bits for representation.

##### Share on other sites
The type of c is GLfixed which is typedef'd as typedef int GLfixed

Thanks.

##### Share on other sites
Just tried your code as you had it there, and it works fine. Unless your compiler has int defined differently than mine (VC6) then there should be no problem. Are you sure it's not just a problem with how you're displaying the results?

##### Share on other sites
Hi,

I am printing it like this, it gives 0 on both occasions :
printf("%d", c);
printf("%d", d);

If I do this however it works fine
printf ("%d", FIX2INT(a));

Thanks

##### Share on other sites
Aside from problems with your compiler (unlikely), there's no reason the code you posted shouldn't work. So there's gotta be something else in the program affecting the result.

The program I tested it with is:

#include <stdio.h>#include <conio.h>#define INT2FIX(x) (x << 16)#define FIX2INT(x) (x >> 16)typedef int GLfixed;int main(int argc, char *argv[]) {	GLfixed a,c,d;	a = 1;	c = INT2FIX(a);	d = FIX2INT(c);	printf("%d\n", c);	printf("%d\n", d);	getch();	return 1;}

Try that and see what you get.

##### Share on other sites
Hmm That code worked fine. Odd. I will compare the code and see.

##### Share on other sites
I'd make it
#define INT2FIX(x) ((x)<<16)
etc.

for extra safety, so you can do
INT2FIX(a+1);

without it evaluating as (a+1<<16) which I think is (a + 65536) due to weird precedence of the shift operators.

##### Share on other sites
Quote:
 Original post by Paradigm Shifterwithout it evaluating as (a+1<<16) which I think is (a + 65536) due to weird precedence of the shift operators.

Shouldn't be a problem since + has higher priority than <<.
Still a good idea to though, otherwise you'll get into trouble with most logic/bitwise operators.

• 18
• 29
• 11
• 21
• 16