Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,961 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,585 people online right now. Registration is fast and FREE... Join Now!




bitwise left shift

 
Reply to this topicStart new topic

bitwise left shift, need help on prog

keefer19
2 Jun, 2007 - 03:08 PM
Post #1

New D.I.C Head
*

Joined: 8 Dec, 2006
Posts: 16


My Contributions
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 :-).


CODE
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;
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Bitwise Left Shift
3 Jun, 2007 - 02:47 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 48 times
Dream Kudos: 550
My Contributions
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
CODE
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.
User is offlineProfile CardPM
+Quote Post

keefer19
RE: Bitwise Left Shift
3 Jun, 2007 - 05:09 AM
Post #3

New D.I.C Head
*

Joined: 8 Dec, 2006
Posts: 16


My Contributions
QUOTE(NickDMax @ 3 Jun, 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
CODE
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 :-)
User is offlineProfile CardPM
+Quote Post

keefer19
RE: Bitwise Left Shift
8 Jun, 2007 - 04:45 AM
Post #4

New D.I.C Head
*

Joined: 8 Dec, 2006
Posts: 16


My Contributions
QUOTE(keefer19 @ 3 Jun, 2007 - 06:09 AM) *

QUOTE(NickDMax @ 3 Jun, 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
CODE
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.
CODE
#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;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:26AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month