我想在两个变量之间执行按位与(&)操作,但是我在double中有一个值.但是double不允许按位运算. [英] I want to perform a bitwise and(&) operation between two variable.but I have one value in double.but double does not allow bitwise opearation.
问题描述
我想在两个数字之间执行按位运算.一个是double类型,另一个是int类型.当我执行代码时,得到的例外是.double和int之间无法执行按位和运算.
我尝试过的事情:
字符串add64 ="0013A2004154EC8E";
double num_add64 = Int64.Parse(add64,System.Globalization.NumberStyles.HexNumber);
int sumadd =(int)sum;
整数r = sumadd& ck;
类型转换后.在sumadd变量中,我得到负值.这就是问题所在.
请帮助我.谢谢您.
I want to perform a bit wise operation between two number.one is of double type and other is of int type.And when i am executing the code getting the exception that.Bitwise and operation can not be performed between double and int.
What I have tried:
string add64 = "0013A2004154EC8E";
double num_add64 =Int64.Parse(add64, System.Globalization.NumberStyles.HexNumber);
int sumadd = (int)sum;
int r = sumadd & ck;
after type casting .In sumadd variable i m getting negative value.That is the problem.
Please help me.Thank you in advance.
推荐答案
为什么要将Int64
分配给double
?
将第二个操作数ck
强制转换为Int64
并执行以下操作:
Why are you assigning theInt64
to adouble
?
Cast the second operandck
toInt64
instead and perform the operation:
Int64 num_add64 =Int64.Parse(add64, System.Globalization.NumberStyles.HexNumber);
int r = Convert.ToInt32(num_add64 & Convert.ToInt64(ck));
因为您要处理不同宽度的整数并执行AND
操作,所以应检查在这里使用uint
是否可能是更好的选择(假设ck
是uint
):
Because you are dealing with integers of different width and performing an AND
operation, you should check if using uint
might be a better choice here (assuming ck
is an uint
):
UInt64 num_add64 =UInt64.Parse(add64, System.Globalization.NumberStyles.HexNumber);
uint r = Convert.ToUInt32(num_add64) & ck;
这篇关于我想在两个变量之间执行按位与(&)操作,但是我在double中有一个值.但是double不允许按位运算.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!