doupaoshu8334 2010-08-13 17:53
浏览 50

按位运算错误?

I'm developping a site for fun and I'm trying to implement a directory access control based on bitwise operators.
I've defined GUEST = 1, GROUP1 = 15 and GROUP2 = 23

If I compare

echo (23 & 1); // print 1 

but if I define GUEST, GROUP1 and GROUP2:

define('GUEST', 1);  
define('GROUP1', 15);  
define('GROUP2', 23);  
// and then  
echo (GROUP2 & GUEST); // print 0  
echo USER_GUEST.','.USER_ROLES1.','.USER_ROLES2; // print 1,15,23` 

With GROUP1 no problem:

echo (GROUP1 & GUEST); print 1.  

Where do I'm wrong ? some suggestion ? Thanks.


I've discovered something really strange: my GUEST, GROUP1 and GROUP2 are declared inside an ini file which I parse by a function parse_ini_file(self::$fileName, true); within a class . After I've parsed the file I define recursively the couples key=value defined under section [DEFINE] (a simply trick). If I comment there my GROUP2=23 definition and I declare it inside the current script (GROUP2 & GUEST) return 1!

  • 写回答

3条回答 默认 最新

  • douping7105 2010-08-13 18:00
    关注

    PHP 5.3:

    php > define('GUEST', 1);
    php > define('GROUP1', 15);
    php > define('GROUP2', 23);
    php > echo GROUP2 & GUEST;
    1
    php > echo GROUP1 & GUEST;
    1
    

    What PHP version are you using?


    Artefacto pointed out a possible string issue (e: but appears to have retracted his post, hm). Again in 5.3:

    php > var_export(GUEST);
    1
    php > var_export(GROUP1);
    15
    php > var_export(GROUP2);
    23
    php > define('GUEST_AS_STRING', '1');
    php > var_export(GUEST_AS_STRING);
    '1'
    php > echo GROUP1 & GUEST_AS_STRING;
    1
    php > echo GROUP2 & GUEST_AS_STRING;
    1
    

    '1' is character 49. 49 & 15 is 1, but 49 & 23 is 17. I'm not convinced that this is a string issue...

    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了