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!

bitwise logic operations

Status
Not open for further replies.

neevor

Programmer
Aug 7, 2002
12
US
I am pretty new to c++ and have had a pretty easy time with it till i got to bitwise logic operations. I tried an example program out of a book I have and can't get it to work.
The code is:

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

int main(int nArg, char* nArgs[ ])
{
cout.setf ( ios::hex);
int nArg1;
cout << &quot;Enter arg1 as a four-digit hexadecimal;&quot;;
cin >> nArg1;

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

int nArg2;
cout << &quot;Enter arg2:&quot;;
cin >> nArg2;
cout << &quot;nArg1 & nArg2 = 0x&quot;
<< (nArg1 & nArg2) << &quot;\n&quot;;
return 0;
}

when i put in a hex number like 0x1234 it will not let me put in the second argument and just gives me an answer.
could you please help?
thank you
 
You don't put in the 0x part. Just type in the 4 digit number in hex.

-Skatanic
 

The only Problem I see with your code it that there are two lines that are duplicated:

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


once they where commented out it worde fine for me.

-Sean
 
I tried the suggestions and now it will take in both variables (thank you for helping with that) but it doesn't treat them as hex, if i put in 00ff first it won't take the second one, and if i put it in second (which i should not have to) it just uses the first one.
 
Hi,

The variables that you have declared are all integers. So they can't accept hex values. Try accepting a character array for the hex value, drop the 1st 2 elements - which is 0x ( U can check to see if it is 0x ) , and then convert the hex to an Integer.

TO do the conversions to Integer , take the characters of the array and multiply it by 16^(their distance from the last element+1) and then add them up.

Now u have an int. Then u can perform integer operations on it and do the reverse process to convert it to hex.

I dont know if u can store values like ffff in int, if u can please someone tell me how to perform operations on it and what are the operational ranges and any other details.

Thankyou
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top