I cant understand this...

Good day! Im a C++ beginner, can anybody explain to me how this 2 codes work?


#define _RGB16BIT565(r,g,b) ((b%32) + ((g%64) << 6) + ((r%32) << 11))

#define _RGB32BIT(a,r,g,b) ( + ((g) << 8) + ((r) << 16) + ( << 24))
#define _RGB16BIT565(r,g,b) is defining a macro called _RGB16BIT565 with three parameters
The second part is some mathematical trick:
1
2
3
4
5
6
7
8
9
10
11
12
13
(
  (b%32)
  + 
  (
    (g%64)
     << 6
  )
  +
  (
    (r%32)
     << 11
  )
)

return the remainder of the division of 'b' by 32 ( somewhat the corresponding digit in base 32 ). See http://en.wikipedia.org/wiki/Modular_arithmetic


same thing
shift the last value by 6 bits ( see http://en.wikipedia.org/wiki/Arithmetic_shift )



same as above
same as above
Last edited on
Strange though - I thought the top one should be ((g%64) << 5) not ((g%64) << 6)
Last edited on
Thanks for clearing that up for me. However im having problem understanding the parenthesis for example:

#define _RGB16BIT565(r,g,b) ((b%32) + ((g%64) << 6) + ((r%32) << 11))

some opening parenthesis has no closing parenthesis which i think will give error but it didn't. Hows this syntax possible? Is creating a macro different?
some opening parenthesis has no closing parenthesis
All parentheses are matched, where do you see miss-matches?
Hows this syntax possible?Is creating a macro different?
Even if it was, you can have whatever you want in a macro
eg:
1
2
3
4
5
6
7
8
9
10
#define MACROS int main (
#define ARE ){
#define BAD return 
#define DONT 0
#define USE ; }
#define THEM


MACROS ARE BAD!!!
DONT USE THEM

Thank you guys!
Topic archived. No new replies allowed.