我对位屏蔽和位操作还很陌生。您能帮我理解这一点吗?我有三个整数a,b和c,并使用以下操作创建了一个新的数字d:
int a = 1;
int b = 2;
int c = 92;
int d = (a << 14) + (b << 11) + c;
我们如何使用d重构a,b和c?
最佳答案
a is 0000 0000 0000 0000 0000 0000 0000 0001
b is 0000 0000 0000 0000 0000 0000 0000 0010
c is 0000 0000 0000 0000 0000 0000 0101 1100
a<<14 is 0000 0000 0000 0000 0100 0000 0000 0000
b<<11 is 0000 0000 0000 0000 0001 0000 0000 0000
c is 0000 0000 0000 0000 0000 0000 0101 1100
d is 0000 0000 0000 0000 0101 0000 0101 1100
^ ^ { }
a b c
So a = d>>14
b = d>>11 & 7
c = d>>0 & 2047
By the way ,you should make sure the b <= 7 and c <= 2047
关于c++ - 使用位掩码重建整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18196914/