Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 11-Jan-18 22:52pm
Comments
Richard MacCutchan 12-Jan-18 4:27am    
This makes no sense, what problem are you trying to solve?
CodeWraith 12-Jan-18 4:57am    
He is wrestling with the same problems as many others: No understanding of the data types and how they are stored in memory, stringly typing and then parsing or converting to some not too well chosen datatype, only to cast it to another not well chosen datatype.

In the end it looks like he simply wants to initialize a variable with a literal 64 bit address and then mask out (guessing!) some part with a checksum.
Richard MacCutchan 12-Jan-18 5:07am    
I know, that is why I posed my question.

1 solution

Why are you assigning the Int64 to a double?

Cast the second operand ck to Int64 instead and perform the operation:
C#
Int64 num_add64 =Int64.Parse(add64, System.Globalization.NumberStyles.HexNumber);
int r = Convert.ToInt32(num_add64 & Convert.ToInt64(ck));

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):
C#
UInt64 num_add64 =UInt64.Parse(add64, System.Globalization.NumberStyles.HexNumber);
uint r = Convert.ToUInt32(num_add64) & ck;
 
Share this answer
 
Comments
Akhand Jyoti 12-Jan-18 5:15am    
Thank u so much.It works.
Jochen Arndt 12-Jan-18 5:36am    
You are welcome.

But note that casting negative integers to a different width will affect the higher bits. That is why I provided my second example when it is sure that values are not negative or should be always treated as unsigned. Which to use here depends on your requirements which I don't know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900