C preprocessor

S

selvesteen

Hello All,

I came across the following macro in a program, could someone explain
what it actually means.

#define BUFFER_MAX_HPN_LEN (2>>21)-1

Is there any other way to achieve the same

Thanks for the help.
Mike
 
C

Chris Dollin

Hello All,

I came across the following macro in a program, could someone explain
what it actually means.

#define BUFFER_MAX_HPN_LEN (2>>21)-1

It means that wherever BUFFER_MAX_HPN_LEN appears in the
following text, it is replaced by `(2>>21)-1`, which looks
like a typo on someone's part to me, since that evaluates
to -1. Are you sure it wasn't `(2<<21)-1`?

The latter would be a way of writing a mask of 1's from
position 21ish rightwards.

<fx:thinks/>
<fx:assume32BitInts>

2 << 21 = 1 << 22 = 1 followed by 22 0's; -1 then gives
us a mask of 21 ones, which is probably why the macro
would be written using 2<<, as the right operand of <<
would be the number of 1's in the mask.

I'd write `((2 << 21) - 1)` myself.

</>
 
P

pete

Hello All,

I came across the following macro in a program, could someone explain
what it actually means.

#define BUFFER_MAX_HPN_LEN (2>>21)-1

Is there any other way to achieve the same

It looks more like a typographical error, than anything else.

BUFFER_MAX_HPN_LEN * 5 is -5

5 * BUFFER_MAX_HPN_LEN is -1



/* BEGIN new.c */

#include <stdio.h>

#define BUFFER_MAX_HPN_LEN (2>>21)-1

int main(void)
{
printf("\nBUFFER_MAX_HPN_LEN * 5 is %d\n\n",
BUFFER_MAX_HPN_LEN * 5);
printf("5 * BUFFER_MAX_HPN_LEN is %d\n\n",
5 * BUFFER_MAX_HPN_LEN);
return 0;
}

/* END new.c */
 
C

CBFalconer

Chris said:
.... snip ...

2 << 21 = 1 << 22 = 1 followed by 22 0's; -1 then gives
us a mask of 21 ones, which is probably why the macro
would be written using 2<<, as the right operand of <<
would be the number of 1's in the mask.

I'd write `((2 << 21) - 1)` myself.

<nit> or '((2uL << 21) -1)' so it works on 16 bitters. </nit>
 
S

snnn

Hello All,

I came across the following macro in a program, could someone explain
what it actually means.

#define BUFFER_MAX_HPN_LEN (2>>21)-1

Is there any other way to achieve the same

Thanks for the help.
Mike

How should the following codes work?

int x;
x=5*BUFFER_MAX_HPN_LEN
x=BUFFER_MAX_HPN_LEN *5


please define "BUFFER_MAX_HPN_LEN" as "((2>>21)-1)" instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top