From time to time I get a fun question. One of them was this: Please provide a list with powers of 2 accompanied by their binary representation, for let’s say up to 50.
First version:
1: int temp=1;
2:
3: for(int i = 1; i<=50; i++)
4: {
5: Console.WriteLine(i+": Dec: "+temp+" - Bin: "+Convert.ToString(temp,2));
6: temp = temp*2;
7: }
This gives the following result:
Here we bump into the maximum of information that can be held by an integer. So for going up to 50, it suffices to change the type of the decimal number to long:
1: long temp=1;
2:
3: for(int i = 1; i<=50; i++)
4: {
5: Console.WriteLine(i+": Dec: "+temp+" - Bin: "+Convert.ToString(temp,2));
6: temp = temp*2;
7: }
So, I could answer the question. But the same problem occurs when you come to the limit of the long-data type (which is at 64).
Double, float or decimal are not supported by the Convert.ToString(…) implementation where you can define the toBase-parameter.
aac0558d-bad5-4ce9-8af7-68c4d115cb67|0|.0