简体   繁体   English

变量赋值的奇怪方式

[英]Strange way of variable assignment

I have been working in C++ and Java and in both the languages I have often times come across a strange way of variable assignment, using bitwise operators. 我一直在使用C ++和Java,在这两种语言中,我经常遇到一种奇怪的变量赋值方式,使用按位运算符。 Instead of what could have been a simple assignment using the assignment operator it is complicated using bit operators like left shift. 使用赋值运算符而不是使用赋值运算符的简单赋值,使用诸如左移的位运算符是很复杂的。 For example, in Java's ServerSocketChannel class we see the following assignments: 例如,在Java的ServerSocketChannel类中,我们看到以下分配:

public static final int OP_READ = 1 << 0;
public static final int OP_WRITE = 1 << 2;
public static final int OP_CONNECT = 1 << 3;
public static final int OP_ACCEPT = 1 << 4;

I am trying to understand here what have we gained by using the << operator. 我试图通过使用<<运算符来理解我们获得了什么。 We could have made simple assignments to assign the variables as 1,4,8,16 respectively as below: 我们可以做出简单的赋值,将变量分别分配为1,4,8,16,如下所示:

public static final int OP_READ = 1;
public static final int OP_WRITE = 4; 
public static final int OP_CONNECT = 8; 
public static final int OP_ACCEPT = 16;  

What is the value add in using << operator here ? 使用<<运算符在这里添加什么值?

This is for clarity/readability (when it matters). 这是为了清晰/可读(当它重要时)。

At the byte code level, OP_ACCEPT = 16 or OP_ACCEPT = 1 << 4 are the same thing (via javap -constants <YourClass> ) 在字节码级别, OP_ACCEPT = 16OP_ACCEPT = 1 << 4是相同的事情(通过javap -constants <YourClass>

It's just easier to see exactly how many times this has been shifted. 更容易确切地看到它被移动了多少次。 Usually this matters when you do different operations that are bound to power of two operations. 通常,当您执行与两个操作相关的不同操作时,这很重要。

On example would be HashMap (or I assume HashXXX structures), where, at least in java, buckets are chosen on the next power of two, always. 例子是HashMap (或者我假设HashXXX结构),其中,至少在java中,总是在下一个2的幂上选择桶。 This simplifies processing or may be, rationalizing, thus number of buckets is declared as : 这简化了处理或者可以合理化,因此桶的数量被声明为:

 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;

At the same time, where, power of two, does not matter, variables are not declared like this: 同时,两个权力无关紧要,变量不是这样声明的:

static final int TREEIFY_THRESHOLD = 8;
static final int MIN_TREEIFY_CAPACITY = 64

Think of how a bucket is chosen for example in case of HashMap via (n - 1) & hash , where n is the number of buckets (always a power of two). 想想如何选择一个桶,例如HashMap via (n - 1) & hash ,其中n是桶的数量(总是2的幂)。 A default capacity of 16 (or better 1 << 4 ), means that the last 4 bits are zero, doing a minus 1 will make them all ones. 默认容量为16(或更好的1 << 4 ),表示最后4位为零,执行减1将使它们全部为1。 So, in a way , 1 << 4 for HashMap would mean that the last 4 bits only are taken into consideration (until next re-hash). 因此, 在某种程度上1 << 4 for HashMap意味着考虑最后4位(直到下一次重新哈希)。 Now think 1 << 28 for example... without shift this would be pretty long to reason about. 现在想想1 << 28例如......没有转变,这将是很长的理由。

A least for me, for such cases, doing for example an or or and would make faster sense on such variables. 一个至少对我来说,这样的情况下,这样的例如orand将使这样的变量,快感。

They're the most efficient way of representing something whose state is defined by several "yes or no" properties. 它们是表示状态由几个“是或否”属性定义的事物的最有效方式。 ACLs are a good example; ACL就是一个很好的例子; if you have let's say 4 discrete permissions (read, write, execute, change policy), it's better to store this in 1 byte rather than waste 4 . 如果您说4个离散权限(读取,写入,执行,更改策略), 最好将其存储在1个字节而不是浪费4

When you left shift 1 bit, the number get multiplied by 2. 当你离开1位时,数字乘以2。

for example take binary number of 5 = 0101 after left shifting 1 time it will become 1010, that is equivalent to 10. 例如,在左移1次后取二进制数5 = 0101它将变为1010,相当于10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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