Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with bitwise logic operations

Status
Not open for further replies.

neevor

Programmer
Aug 7, 2002
12
US
I am new to c++ and have had no problems until i got to bitwise logic operations. I compiled the code from a book i have and i can't get it to work right. I can get it to take in two variables but it won't treat them like hex numbers. If i enter someting like 00ff as an argument it won't work. If you can help me thank you very much.



#include <stdio.h>
#include <iostream.h>

int main(int nArg, char* nArgs[])
{
//set output format to hexadecimal
cout.setf(ios::hex);

//input the first argument
int nArg1;
cout << &quot;Enter arg1 as a four-digit hexadecimal: &quot;;
cin >> nArg1;

int nArg2;
cout << &quot;Enter arg2: &quot;;
cin >> nArg2;

cout << &quot;nArg1 & nArg2 = 0x&quot;
<< (nArg1 & nArg2) << &quot;\n&quot;;

cout << &quot;nArg1 | nArg2 = 0x&quot;
<< (nArg1 | nArg2) << &quot;\n&quot;;
cout << &quot;nArg1 ^ nArg2 = 0x&quot;
<< (nArg1 ^ nArg2) << &quot;\n&quot;;

return 0;
}
 
If you are expecting to read (enter at keyboard) hex values, then you have to set cin to hex mode also. Currently you are only setting cout to hex mode.

The way you currently have it, it input will only except base 10 digits (0-9), so the entry of 00ff will fail upon the first 'f'.

Either add a cin.setf(ios::hex); or when running as it is, enter decimal values (255 for 0xff).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top