-5

theres an exam task and im wondering how to solve that properly.

Task:

We have to write the output of this code as the solution.

int iq = -99887766;
        string pout = "";
        int mask = 1 << 31;
        for (int n = 1; n <= 32; n++)
        {
            pout += (iq & mask) == 0 ? "0" : "1";
            iq <<= 1;
            if (n % 4 == 0)
            {pout += " ";}
        }
        Console.WriteLine(pout);

My suggestions:

First transfer the -99887766 to binary:

  1. 9988776 in binary = 0000 0101 1111 0100 0010 1010 1001 0110

  2. revert the bits and add 1 = 1111 1010 0000 1011 1101 0101 0110 1010

Second transfer the int mask to binary and bitshift (left) by 31

  1. mask = 0000 0000 0000 0000 0000 0000 0000 0001

  2. after bitshift = 1000 0000 0000 0000 0000 0000 0000 0000

Third In the for statement, both ints are calculated bitwise by "&". thats clear to me, for instance:

(1111 1010 0000 1011 1101 0101 0110 1010 & 1000 0000 0000 0000 0000 0000 0000 0000) != 0 so the string is filled with 1.

then I bitshift iq by 1:

1111 1010 0000 1011 1101 0101 0110 1010 -> 1111 0100 0001 0111 1010 1010 1101 0100

and do the same operation again whichs leads to a 1 stored in the string again and so on.

the output at the end should be 32 numbers in the string with 0 or 1 and every fourth number there is a blank.

Is this the correct way to solve this task?

Is there any trick to do it faster because this is only giving 10 points meaning we should solve this within 10 minutes!

rene
  • 41,474
  • 78
  • 114
  • 152
Julian Herbold
  • 537
  • 9
  • 20
  • 1
    It's unclear what your actual question is. You posted some code and explained it. Where's the problem? – sloth Jun 18 '14 at 08:54
  • well, if this is the right and the fastest way to do it, since there isnt a lot of time in the exam ;) – Julian Herbold Jun 18 '14 at 09:06
  • So the question is "What's the output in the console from this code?". And you want to check if what you have done is correct and the best possible solution? – James Jun 18 '14 at 09:26
  • 2
    Assuming internet access is allowed during the exam, I'm pretty sure you've been given access to quickly look up resources that can help you, not to have you post the exam questions and ask for help to solve it. The fact that the question should be solvable by you within 10 minutes seems to support that. It's similar to an open book exam: you still need to study for such exams and know the material, the book is for when you can't remember the details but know exactly what to look for so it doesn't take you a lot of time. – user247702 Jun 18 '14 at 09:33
  • @ Stijn No the exam is in 2 weeks ;) we can take notes with us but are not allowed to use the internet. But since the time is very short, you cant pass without preparation. Thats what the question is for ;) – Julian Herbold Jun 18 '14 at 09:36
  • @ James Barras I agree with you, its easy to check the results in the computer. But I thought there is maybe a faster way to solve this task since there is only 10 minutes planned and since we cant use technical equipment besides non-programmatically calculators. – Julian Herbold Jun 18 '14 at 09:39

1 Answers1

2

What does this code do? How would have you named such a function? I would have named it so

/// <summary>
/// Convert an Int32 value into its binary string representation.
/// </summary>
/// <param name="value">The Int32 to convert.</param>
/// <returns>The binary string representation for an Int32 value.</returns>
public String ConvertInt32ToBinaryString(Int32 value)
{
        String pout = "";    
        int mask = 1 << 31;

        for (int n = 1; n <= 32; n++)
        {
            pout += (value & mask) == 0 ? "0" : "1";
            value <<= 1;
            if (n % 4 == 0)
            {pout += " ";}
        }

        return pout;
}

The question in your exam could be rephrased as "give me binary representation of -99887766 in 32 bit two-complementary code".

There is no very simple and fast conversion to binary but 10 minutes are quite enough for a value below 10 millions. There are few methods but on paper I usually prefer "Descending Powers of Two and Subtraction" over "Short Division by Two with Remainder" http://www.wikihow.com/Convert-from-Decimal-to-Binary.

Just don't (already having binary representation in second step) make those bit comparisons with bit mask.

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • we have to do everything by hand, so thx a lot for the article. its faster and safer to transfer the decimal to binary by "Short Division by Two with Remainder" than by the division of 2. but what do you mean by "Just don't (already having binary representation in second step) make those bit comparisons with bit mask." ? Since there is the "&" operator, I have to comparer the bits of both integers or not? – Julian Herbold Jun 18 '14 at 10:01
  • 1
    The original binary number after step 2 is already the answer. – Eugene Podskal Jun 18 '14 at 10:10
  • You are right, thats exactly the answer is was hoping to get. It isnt necessary to compare both int binary since the solution is already there. So actually the method converts the int into binary. By knowing that the task is answered within a few minutes, thx a lot! – Julian Herbold Jun 18 '14 at 11:30