简体   繁体   中英

What does 'int kk = 2 | 3;' mean?

I have been programming in java for a while but I have not met a strange expression

int kk = 2 | 3;

What does '|' mean in this expression? It seems hard to Google it.

I met it in source

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

Why we need to use this?

It's a bitwise or - each result bit will be set if either or both inputs have a bit set in that position. 2 is 10 in binary, 3 is 11, so the result will also be 3.

| is a bitwise Operator . In your case, 2|3 will produce 3 as 2 is 10 and 3 is 11. 10 | 11 = 11 10 | 11 = 11 .

您可以查看http://vipan.com/htdocs/bitwisehelp.html我认为它们是关于位移运算符的非常好的教程

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM