int main() { int num1, num2, num3, firstNumber, secondNumber; num1=0xc0000000; num2=0x80000000; num3=0x40000000; cout<<"Enter the first hexadecimal number."<<endl; // enter the first number cin>>firstNumber; cout<<"Enter the second hexadecimal number."<<endl; // enter the second number cin>>secondNumber; if (firstNumber>=num1) //compares the char to num1 { //if it is larger than num1 if shifts firstNumber = (firstNumber <<2) + 3; //and adds 3 } else if(firstNumber<num1 && firstNumber>=num2) //compares the char between num1 and num2 { //if it is true then shifts and adds 2 firstNumber = (firstNumber <<2) + 2; } else if(firstNumber<num2 && firstNumber>=num3) //compares the char between num2 and num3 { //it it is true then it shifts and adds 1 firstNumber = (firstNumber <<2) + 1; } else { firstNumber = firstNumber <<2; //shifts the number }; firstNumber=firstNumber ^ secondNumber; //makes the firstNumber equal //to an XOR of the other number cout<<hex<<firstNumber<<endl; //prints the finalString in hex. return 1; }
3 Replies  17158 Views  Last Post: 08 June 2007  05:45 AM
#1
bitwise left shift
Posted 02 June 2007  04:08 PM
Hello, i am fairly new to c++ programming and need some help with a program. We are supposed to enter two 8 digit hexadecimal numbers, preform a circular left shift on the first number then exclusive or it with the second number. We are then supposed to print the answer in hex. I've written the code below but it doesn't seem to be returning the correct answers. I am comparing the first number to 3 different hex numbers so I know if the first two bits were 1's. What am I missing/doing wrong? Any help will be greatly apricated :).
Replies To: bitwise left shift
#2
Re: bitwise left shift
Posted 03 June 2007  03:47 AM
Although this is not a great way to do it, your logic does seem correct. The problem I am having with your code is inputing the hex number. I added cin.setf(ios::hex, ios::basefield); which should allow you to enter hex numbers, and it does for small numbers, but seems to choke with large numbers... not sure what the problem there is.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8  3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
I will look into the input... not sure what is wrong off the top of my head.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8  3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
int RotateBitsRight(int num, int numBits) { int temp; int wordSize = 8 * sizeof(int); //Calculate how many bits in an int int Shift = wordSize  numBits; temp = (unsigned)num << Shift; num = ((unsigned)num >> numBits) + temp; return num; }
I will look into the input... not sure what is wrong off the top of my head.
#3
Re: bitwise left shift
Posted 03 June 2007  06:09 AM
NickDMax, on 3 Jun, 2007  03:47 AM, said:
Although this is not a great way to do it, your logic does seem correct. The problem I am having with your code is inputing the hex number. I added cin.setf(ios::hex, ios::basefield); which should allow you to enter hex numbers, and it does for small numbers, but seems to choke with large numbers... not sure what the problem there is.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8  3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
I will look into the input... not sure what is wrong off the top of my head.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8  3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
int RotateBitsRight(int num, int numBits) { int temp; int wordSize = 8 * sizeof(int); //Calculate how many bits in an int int Shift = wordSize  numBits; temp = (unsigned)num << Shift; num = ((unsigned)num >> numBits) + temp; return num; }
I will look into the input... not sure what is wrong off the top of my head.
Thank you very much for the help. I reworked my code using your insights and have it running. Thanks again :)
#4
Re: bitwise left shift
Posted 08 June 2007  05:45 AM
keefer19, on 3 Jun, 2007  06:09 AM, said:
NickDMax, on 3 Jun, 2007  03:47 AM, said:
Although this is not a great way to do it, your logic does seem correct. The problem I am having with your code is inputing the hex number. I added cin.setf(ios::hex, ios::basefield); which should allow you to enter hex numbers, and it does for small numbers, but seems to choke with large numbers... not sure what the problem there is.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8  3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
I will look into the input... not sure what is wrong off the top of my head.
As for rotating bits. There is a better way. Lets say I wanted to rotate a byte worth of data 3 places to the left:
byte = 1100 0011
temp = byte >> (8  3) = byte >> 5 = 0000 0110
byte = byte << 3 + temp = 0001 100 + 0000 0110 = 0001 1110
What we do is use a temp variable to hold the bits that would have gotten erased in the shift, then we add them back in.
here is an example of a right rotate
int RotateBitsRight(int num, int numBits) { int temp; int wordSize = 8 * sizeof(int); //Calculate how many bits in an int int Shift = wordSize  numBits; temp = (unsigned)num << Shift; num = ((unsigned)num >> numBits) + temp; return num; }
I will look into the input... not sure what is wrong off the top of my head.
Thank you very much for the help. I reworked my code using your insights and have it running. Thanks again :)
Here is the final version of the code that I turned in. Thanks again for the hint.
#include <iostream> #include <iomanip> using namespace std; int main() { unsigned long int temp,firstNumber,secondNumber; cout<<"Enter two hexadecimal numbers separated by a space."<<endl; cin>>hex>>firstNumber>>secondNumber; temp=firstNumber>>30; firstNumber=((firstNumber<<2) + temp) ^ secondNumber; cout<<hex<<firstNumber<<endl; return 1; }
Page 1 of 1
