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:
9988776 in binary = 0000 0101 1111 0100 0010 1010 1001 0110
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
mask = 0000 0000 0000 0000 0000 0000 0000 0001
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!