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

2019-06-11 07:56发布

问题:

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
        }