Stéphane
Stéphane

Reputation: 153

Result of bitwise OR operator in c

I have a question about bitwise OR | operator from c to rust.

I have this lot of define to convert from c to rust, and i’m not sure of the flag SEFLG_ASTROMETRIC.

#define SEFLG_JPLEPH    1       /* use JPL ephemeris */
#define SEFLG_SWIEPH    2       /* use SWISSEPH ephemeris */
#define SEFLG_MOSEPH    4       /* use Moshier ephemeris */

#define SEFLG_HELCTR    8      /* heliocentric position */
#define SEFLG_TRUEPOS   16     /* true/geometric position, not apparent position */
#define SEFLG_J2000 32     /* no precession, i.e. give J2000 equinox */
#define SEFLG_NONUT 64     /* no nutation, i.e. mean equinox of date */
#define SEFLG_SPEED3    128    /* speed from 3 positions (do not use it,
                                * SEFLG_SPEED is faster and more precise.) */
#define SEFLG_SPEED 256    /* high precision speed  */
#define SEFLG_NOGDEFL   512    /* turn off gravitational deflection */
#define SEFLG_NOABERR   1024   /* turn off 'annual' aberration of light */
#define SEFLG_ASTROMETRIC (SEFLG_NOABERR|SEFLG_NOGDEFL) /* astrometric position,
                                * i.e. with light-time, but without aberration and
                    * light deflection */
#define SEFLG_EQUATORIAL (2*1024)    /* equatorial positions are wanted */
#define SEFLG_XYZ   (4*1024)     /* cartesian, not polar, coordinates */
#define SEFLG_RADIANS   (8*1024)     /* coordinates in radians, not degrees */
#define SEFLG_BARYCTR   (16*1024)    /* barycentric position */
#define SEFLG_TOPOCTR   (32*1024)    /* topocentric position */
#define SEFLG_ORBEL_AA SEFLG_TOPOCTR /* used for Astronomical Almanac mode in
                                      * calculation of Kepler elipses */
#define SEFLG_SIDEREAL  (64*1024)    /* sidereal position */
#define SEFLG_ICRS  (128*1024)   /* ICRS (DE406 reference frame) */
#define SEFLG_DPSIDEPS_1980 (256*1024) /* reproduce JPL Horizons
                                      * 1962 - today to 0.002 arcsec. */
#define SEFLG_JPLHOR    SEFLG_DPSIDEPS_1980
#define SEFLG_JPLHOR_APPROX (512*1024)   /* approximate JPL Horizons 1962 - today */

1024 | 512

I tried with the calculator of my mac 1024 OR 512 and the result is 1536. Is this result correct ?

Upvotes: 3

Views: 85

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

Yes, the result is correct: in fact, OR-ing 1024 and 512 yields the same number as adding them.

This is a convenient "shortcut" for OR-ing powers of two: since their digits never occupy the same position, addition is the same as "OR".

The same principle is in play here as with adding decimal numbers with zeros in all but a single position, when non-zero positions do not overlap: adding, say, 6000+900+30+7 is the same as writing down the individual digits into 6937, because place values do not interact with each other. OR-ing distinct powers of two in binary is the same as writing down all ones in positions designated by numbers being OR-ed.

Note that the expression (SEFLG_NOABERR|SEFLG_NOGDEFL) is evaluated at compile time, so replacing it with 1536 is not going to yield any run-time improvement.

Upvotes: 2

dan1st
dan1st

Reputation: 16477

Yes it is correct.

512 =01000000000
1024=10000000000
1536=11000000000

OR-Calculation:

 10000000000
|01000000000
=11000000000

Upvotes: 0

Mureinik
Mureinik

Reputation: 312136

In a word - yes. Note that 1024 and 512 don't have "1" bits in the same positions, so the result of bitwise-oring them is the same as adding them.

Upvotes: 0

Related Questions