How to read result of bitwise operator OR (|)?

Deadly 提交于 2020-01-03 05:52:05

问题


I would like the confirmation on bitwise operators inside Android XML files. For example this line

android:layout_gravity="center_horizontal|bottom"

How should I read it? Are the rules inherited from Java or Android does its own interpretation of bitwise operators?

Are all bitwise operators supported? If yes, could you show me the example of other operators (link is fine as well)?

Thanks


回答1:


The value of android_layout_gravity is parsed by Android code. As documented here, the only recognized operator here is |. I don't believe that interpreting | as bitwise OR operator is part of the spec. (For instance, center_horizontal is 0x05 and right is 0x01; their bitwise OR would just be 0x05.)




回答2:


2|1 = 3
2&1 = 0
3&1 = 1

central_horizontal, bottom are integers so you can use this construction

    int center_horizontal = 0x05; //just for clarification
    int other_orientation = 0x10;
    int orietntation = center_horizontal | other_orientation;

//this condition returns true
    if((orientation&center_horizontal)==center_horizontal){
        //something to do
        }


来源:https://stackoverflow.com/questions/6457837/how-to-read-result-of-bitwise-operator-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!