Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two Hexadecimal values in C++

Tags:

c++

I want to compare two hexadecimal(stored in long) below is my code

long constant = 80040e14;
if(constant == 80040e14)
    cout<<"Success"<<endl;
else
    cout<<"Fail!!"<<endl;

In this code flow control always returns to else part, can anyone please suggest how to proceed with the comparison.

Thanks

Santhosha K

like image 541
Santhosha Avatar asked Jul 29 '10 10:07

Santhosha


2 Answers

Prefix your constants with '0x'.

Your constant only has 'e' in it and the compiler will treat numbers of the form: NNNeEEE as scientific notation. Using the '0x' prefix tells the compiler that the following characters are in hexadecimal notation.

In your code, 80040e14 is 8004000000000000000 which is way too big to fit into 32bit value but can fit into a 64bit value. But, 80040e14 is a floating point number so the comparison will convert the long to a float to make it the same type as the constant and so the two values will be different due to the complexities of floating point code.

like image 59
Skizz Avatar answered Sep 29 '22 06:09

Skizz


You need to put 0x in front of your hexadecimal numbers in C++

like image 35
John Sibly Avatar answered Sep 29 '22 05:09

John Sibly