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 SkipVought 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
0
0
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;
}
 
I suspect the problem only occurs when you enter an argument with a-f included, when arg1 & arg2 contain only numeric digits it works OK,

You are trying to enter non-numeric data into an integer - this would appear to be where your problem lies.

Cheers
 
thanks,
do you know how to make a variable accept hexadecimal integers
 
You would have to read it in as a string, convert it into an integer, do the bitwise math then convert the integer result back to hex for the output phase....
 
ok, thanks,
do you know why this code works with Visual C++ on windows,
but not on freebsd? I was wondering what the compiler diffrences were.
 
Unfortunately I can't help you there, I have Windows (2000, NT & 95), Linux (various), Solaris (7 & 8) , AIX 4.3.3 and OS/390 to play with but not FreeBSD - sorry.
 
I have mandrake linux and i can't get it to work on that either.
 
To input a hex number add in the word hex. eg

cin >> hex >> nArg1;

iostream.h is the old version. You should really be using iostream.

#include <iostream>
using namespace std;
int main()
{
//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 >> hex >> nArg1;

int nArg2;
cout << &quot;Enter arg2: &quot;;
cin >> hex >> 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;
}
 
Thank you, this solved the problem of it not taking the hex numbers, but it is giving me the wrong answers?
The first answer for the set
nArg1 = 0x1234
nArg2 = 0x00ff
should be
0x34
i am getting
0x52
 
You need to modify the output statements to format the output in hex, you are currently showing them as decimal values...

Try these changes and it should now work.

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

Cheers - Gavin
 
Yeeahh!!
Worked perfect,
thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top