Thread: Need help.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    21

    Need help.

    So this is what the assignment says:

    - I need to write two whole numbers using signed int
    - I need to save these two numbers in a unsigned long long variable with the help of Bitshift
    - Print the new number that was created

    Now there are more lines but the one that confuses me is the second, I have no clue how to do whats asked there, also im not sure whats the new number that I need to print.

    In a example how it needs to look in the end the new number for them is e.g. 188978561112 with the numbers 44 and 88

    I really dont understand how to save two numbers in a variable... And dont get it how Bitshift should help here.

    Thank you
    Last edited by JohnnyC; 10-28-2018 at 03:51 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start by printing out 188978561112, 44 and 88 in hexadecimal.

    It'll help you understand how they relate to each other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    44 in hex 0x2C
    88 in hex 0x58
    188978561112 in hex 0x2C00000058

    So what I got from this is that I gotta move the first number 32 bits to the left so <<32 and then add the second number and yea that works.

    Code:
    int main()
    {
        signed int a;
        signed int b;
        unsigned long long  c;
        scanf("%i \n %i",&a, &b);
        c = (((unsigned long)a <<32)+b);
        printf("First number %d \nSecond number %d\n",a,b);
        printf("%llu \n",c);
            
    }
    But Im not sure what to do if the second number is negative.

    I cant get the correct number there is an one more example.

    e.g. First number 20, Second number -33 result 90194313183

    in hex thats 0x14, -0x21 reslut 0x14FFFFFFDF

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider using | rather than + to combine the two values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    Did but I still dont get the correct result for 20 -33
    18446744073709551583

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by JohnnyC View Post
    Did but I still dont get the correct result for 20 -33
    18446744073709551583
    Is that the result you want or the result you're getting?

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    Thats the result I'm getting when I put 20 and -33 using | instead of +.
    If I use + I get 85899345887.

    While I should be getting 90194313183

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, 20 is 0x14 and -33 is (at least on my machine) 0xffffffdf, and 90194313183 is 0x14ffffffdf, so that seems like a reasonable choice.

    Presumably what's happening is that your -33 is getting promoted to long, and therefore the top 32 bits are getting filled with ones instead of zeroes because of the sign bit; to make that not happen you should mask off the top thirty-two bits of b.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Son?!? ... Anyway, how about this:
    Code:
    #include <stdio.h>
    int main() {
        int a = -0x12345678, b = 0x12345678;
    
        printf("%x %x\n", a, b);
    
        long long n = 0;
        n |= (unsigned)a;
        n <<= 32;
        n |= (unsigned)b;
    
        printf("%llx\n", n);
    
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    Long lost father?

    But yea it works Thanks a lot

    Code:
    int main()
    {
        signed int a;
        signed int b;
        scanf("%i \n %i",&a, &b);
        unsigned long long c = 0;
        c = c | (unsigned int)a;
        printf("Deci%lld\n",c);
        printf("Hex%llx\n\n",c);
        c = c << 32;
        printf("Deci%lld\n",c);
        printf("Hex%llx\n\n",c);
        c = c | (unsigned int)b;
        printf("Deci%lld\n",c);
        printf("Hex%llx\n\n",c);
        
        printf("First number %d \nSecond number %d\n",a,b);
        printf("%lld \n",c);
        
            int (add_special_type) = (a+b);
            {
                printf("%lld\n",(long long)add_special_type);        
            }    
    }
    Some of the printf I put just to see whats it doing so I can better understand it.
    And the int add_special_type, that was also in the assignment so yea.
    Thank you guys

    Without the printf
    Code:
    int main()
    {
        signed int a;
        signed int b;
        scanf("%i \n %i",&a, &b);
        
        unsigned long long c = 0;
        c = c | (unsigned int)a;
        c = c << 32;
        c = c | (unsigned int)b;
    
        printf("First number %d \nSecond number %d\n",a,b);
        printf("%lld \n",c);
        
            int (add_special_type) = (a+b);
            {
                printf("%lld\n",(long long)add_special_type);        
            }    
    }

Popular pages Recent additions subscribe to a feed

Tags for this Thread