Thread: got stuck with assignment ..

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    got stuck with assignment ..

    Hi there,
    i have an assignment that required doing operations with hexadecimal numbers.
    here is the instructions:

    add == add num1 to num2
    sub == sub num1 from num2
    and == bitwise and of the two numbers
    or == bitwise or of the two numbers
    xor == bitwise exclusive or of the two numbers
    shl == shift the bits of num1, num2 places to the left
    shr == shift the bits of num1, num2 places to the right
    asr == arithmetic shift right the bits of num1, num2 places to the right
    rol == rotate the bits of num1, num2 places to the left
    ror == rotate the bits of num1, num2 places to the right

    examples:
    Type in an expression: 49 add 41
    Answer is: 8a

    Type in an expression: 2a xor 7d
    Answer is: 57

    Type in an expression: 49 ror 10
    Answer is: 490000

    Type in an expression: 80000002 asr 1
    Answer is: c0000001
    -------------------------------------

    I got stuck with ( asr , rol and ror). i know that i need to use (&) with the hexadecimal number first and store it in a mask and proceed with the operation and finally i need to use ( | ) the mask with the final result. but how ?????
    here is what i have done so far

    Code:
    int main(){
    	char op[3];
    	int num1, num2, result;
    	printf("Type in an expression: ");
    	scanf("%x%s%x", &num1, op, &num2);
    	if ( strcmp(op, "add") == 0){
    		result = num1 + num2;
    		printf("The answer is: %x", result);
    	}else if (strcmp(op, "sub")==0){
    		result = num1 - num2;
    		printf("The answer is: %x", result);
    	}else if( strcmp(op, "and")==0){
    		result = num1 & num2;
    		printf("The answer is: %x", result);
    	}else if( strcmp(op, "xor")==0){
    		result = num1 ^ num2;
    		printf("The answer is: %x", result);
    	}else if(strcmp(op, "or")==0){
    		result = num1 | num2;
    		printf("The answer is: %x", result);
    	}else if( strcmp(op, "shl")==0){
    		while(num2 > 0){
    			num1 = num1 << 1;
    			num2--;
    		}
    		printf("The answer is: %x",num1);
    	}else if( strcmp(op, "shr")==0){
    		if ( num1 = 0xffffffff){
    			num2--;
    			num1 = 0x7fffffff;
    		}
    		while(num2 > 0){
    			num1 = num1 >> 1;
    			num2--;
    		}
    		printf("The answer is: %x", num1);
    	}
    }
    please help me..

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Note that in C, the behavior of the shift operators << and >> is undefined for signed integers. You should make num1 and num2 unsigned for any shift-related operations.

    You can simplify your shl and shr by doing something like this:
    Code:
    num1 <<= num2;  // this shifts num1 left by num2 bits
    As for your ror/rol, you need to do something like (assuming 32-bit numbers):
    Code:
    mask off num2 bits from the left side of num1 and shift them right 32-num2 bits
    mask off 32-num2 bits from the right side of num1 and shift them left num2 bits
    bitwise-or those two results
    
    e.g.
    9a7261d3 rol 4
    ((0xf0000000 & num1) >> (32-num2)) | ((0x0fffffff & num1) << num2)
    ans = 0xa7261d39
    The asr is probably the toughest. The idea is a shift right, with sign extension. One approach is (again, assuming 32 bit numbers):
    Code:
    if the sign bit of num1 is a 1
        sign_ext = all 1 bits, shifted left by 32-num2 bits
    else
        sign_ext = 0
    shift num1 right by num2 bits
    bitwise or sign_ext with num1
    
    e.g.
    9a7261d3 asr 8
    (0xffffffff << (32-num2)) | (num1 >> num2)
    ans = 0xff9a7261
    Last edited by anduril462; 03-31-2011 at 01:21 AM. Reason: Fixed example to use rol instead of ror.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Bug in program: you've got char op[3] but expect it to hold up to three letter mnemonic. Three character string needs four elements to account for trailing null.

    Since you're scanf()'ing with no restriction on string length there, even op[4] could easily be overrun by busy hands. I think %3s might be a way to limit the string length but that may cause other issues reading the following parameter. Just a heads up to get you thinking about potential bug issues.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    sorry i didn't mention that the program for unsigned number, you are right.
    for the ror and the rol , i didn't understand ( 32-num2 bits).
    as i understand , for rolling to the left i need to see if the most significant is one eg. num1 = [10101000]
    u know these thing better than me so i should use (&) with num1 and assign it in a new variable let's call it left. so left = num1 & 0x80; BUT THE PROBLEM IS HOW CAN I KNOW IF (num1) 8-bits or 16-bits or 32-bits. I MEAN THE INPUTS. because the teacher said all inputs are within ( 0x00 to 0xffffffff) i hope my explanation is quite clear.

    .. am really frustrated 2morow is the due date.

    THANKS A LOT BOOS,

    also another Question ,
    what is the difference between (ror) and (asr) .
    examples would be a nice thing..
    thank u again
    Last edited by FrozenMind; 03-31-2011 at 12:38 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by FrozenMind View Post
    sorry i didn't mention that the program for unsigned number, you are right.
    for the ror and the rol , i didn't understand ( 32-num2 bits).
    So if num2 is 7, you are rotating num1 7 bits to the right or left. Thus, 32-num2 bits is 32-7 bits, or 25 bits. I just meant subtract the number in num2 from 32 and that's how much you will << or >>.

    BUT THE PROBLEM IS HOW CAN I KNOW IF (num1) 8-bits or 16-bits or 32-bits. I MEAN THE INPUTS. because the teacher said all inputs are within ( 0x00 to 0xffffffff) i hope my explanation is quite clear.
    My assumption would be that all numbers should be treated as 32 bits. Thus, you would think of the number 0x4f as 0x0000004f.

    .. am really frustrated 2morow is the due date.
    Next time get an earlier start, and ask your professor/TA or come here sooner if you have questions.

    also another Question ,
    what is the difference between (ror) and (asr) .
    ror means ROtate Right. It's like a shift where whatever bits get shifted off the right side get put in the spaces on the left side. asr means Arithmetic Shift Right. Basically, a shift right is like dividing by two, and a shift left is like multiplying by two. This gets complicated when you have signed numbers, and a regular shift will make negative numbers turn into other, positive numbers. The arithmetic shift keeps the negative numbers negative and preserves the multiply/divide by two behavior.

    examples would be a nice thing..
    thank u again
    I gave examples in my previous post. They're what follows the e.g. in each code block. I fixed a small typo (highlighted) in the first example. Pay attention to what happens to the 9 in that one to see the rotate effects. In the second one, notice how two f's got shifted in instead of two 0's. That's the arithmetic shift keeping a negative number negative.

    You really need to read up on bitwise operators and 2's compliment to understand where these come into play. Google should turn up plenty of resources on those.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    sizeof(int) will tell you how many bytes are in that datatype. (so 2 equals 16 bits, etc).

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Thank you so so so much anduril462.. i really liked your way of subtracting num2 from 32..
    also sorry for keep nagging about the examples.
    thank you nonoob & Adak.
    sizeof(int) is new for me
    thanks guys again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Stuck With C Assignment
    By adj123 in forum C Programming
    Replies: 1
    Last Post: 03-04-2008, 07:14 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM