首页 > 解决方案 > c中按位或运算符的结果

问题描述

我有一个关于按位或的问题 | 运算符从 c 到 rust。

我有很多定义可以从 c 转换为 rust,但我不确定 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

我用我的mac 1024 OR 512的计算器试过了,结果是1536。这个结果正确吗?

标签: cbit-manipulationlogical-operators

解决方案


是的,结果是正确的:事实上,OR-ing 1024 和 512 产生的数字与将它们相加的数字相同。

这是两个的 OR-ing 幂的一个方便的“捷径”:因为它们的数字从不占据相同的位置,所以加法与“OR”相同。

当非零位置不重叠时,这里的原理与在除单个位置之外的所有位置添加零的十进制数相同:添加,例如,6000+900+30+7 与写下单个数字相同进入 6937,因为位值不相互影响。在二进制中对 2 的不同幂进行 OR-ing 与在被 OR-ed 的数字指定的位置上写下所有的 1 相同。

请注意,表达式(SEFLG_NOABERR|SEFLG_NOGDEFL)是在编译时评估的,因此将其替换为 1536 不会产生任何运行时改进。


推荐阅读