Macros issues

Started by
7 comments, last by doynax 19 years, 6 months ago
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
The more applications I write, more I find out how less I know
Advertisement
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.
The type of c is GLfixed which is typedef'd as typedef int GLfixed

Thanks.
The more applications I write, more I find out how less I know
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?
-----BEGIN GEEK CODE BLOCK-----Version: 3.12GCS/M/S d->+(++) s+: a19? C++++ UL++ P+++ L+ !E W+++ N+ o++ K? w!O M-- V? !PS PE Y+ PGP t++ 5+++ X R tv+> b+(++)>+++ DI+++>+++++ D++G e>++++ h! r y?------END GEEK CODE BLOCK------
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
The more applications I write, more I find out how less I know
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.
-----BEGIN GEEK CODE BLOCK-----Version: 3.12GCS/M/S d->+(++) s+: a19? C++++ UL++ P+++ L+ !E W+++ N+ o++ K? w!O M-- V? !PS PE Y+ PGP t++ 5+++ X R tv+> b+(++)>+++ DI+++>+++++ D++G e>++++ h! r y?------END GEEK CODE BLOCK------
Hmm That code worked fine. Odd. I will compare the code and see.
The more applications I write, more I find out how less I know
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.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Quote:Original post by Paradigm Shifter
without 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.

This topic is closed to new replies.

Advertisement