Binary Representation

  • Thread starter Thread starter D
  • Start date Start date
D

D

I want to show the binary equivalentcy of the numbers 1 -
256. I am writing my program as a console application
and I do not want to use any String formats. Can anyone
please get me started on the right track. Thank you in
advance.

D
 
Hello,

D said:
I want to show the binary equivalentcy of the numbers 1 -
256. I am writing my program as a console application
and I do not want to use any String formats. Can anyone
please get me started on the right track.

\\\
Dim i As Integer
For i = 1 To 256
Debug.WriteLine(Convert.ToString(i, 2))
Next i
///
 
first of all thank you for your guidence. I had to first
write the code as follows for it to display:

Dim i As Integer
For i = 1 To 256
Console.WriteLine(Convert.ToString(i, 2))
Next i

However, I found that some of the numbers were
represented with only 6 bits while others showed being
represented as 9 bits and so on. also do you know of a
way to print the results without using the "ToString"

Thank you so much already. You have been a great help.
 
Hi D,

It's possible to do this by testing each bit in turn and outputting either a "0" or a "1" but it's a lot of work compared to
using the built-in functions.

Why aren't you allowed to use string functions? What's it all for?

Regards,
Fergus
 
Hey Fergus

It is for not particular reason other than the fact that
is how I want to do it. maybe you could look at my code
and tell me what I'm doing wrong. I want to have 8
columns but I seem to have my formatting all wrong.

Derrick
-----Original Message-----
Hi D,

It's possible to do this by testing each bit in turn
and outputting either a "0" or a "1" but it's a lot of
work compared to
 
I have no idea what you are trying to achieve with the side, row and column
stuff but, if I interpret your 'problem correctly what you looking for is to
have each value shown as 'binary' with all 8 bits displayed thus:

1 = 00000001
256 = 11111111

This can be simply achieved by using:

Convert.ToString(counter, 2).PadLeft(8, "0"c)

Your restriction on using string functions is invalid because
Console.WriteLine is, effectivly, a string function.

Looks like some sort of acedemic assignment to me.
 
Actually, this very much sounds like a classroom project that has been
assigned, so here's the spoiler...

Sub Main()
Console.WriteLine("Binary Table")
Console.WriteLine()

Dim counter As Integer
Dim bitpos as Integer

For counter = 1 to 256
For bitpos = 8 to 0 Step -1
If (counter And (2^bitpos)) > 0
Console.Write("1")
Else
Console.Write("0")
End If
Next
Console.WriteLin()
Next

End Sub

The bitpos variable is 8 to 0 because you want the bitrep for 256, which
exceeds the 255 8-bit limit.
 
Hi Derrick,

|| While row <= side 'control row
|| column = 1
|| While column <= side
|| For counter = 1 To 256
|| Console.WriteLine(Convert.ToString(counter, 2))
|| column += 1
|| Next counter
|| End While
|| Console.WriteLine()
|| row += 1
|| End While

You've got the right idea with wanting two nested loops. However, as you've found, you're getting loads of consecutive lists of
256.

What you need is to do is 8 values horizontally on the same line. Then go to the next line and do the next 8. And so on. You use
Console.Write() when you want to go stay on the same line. Your Console.WriteLine() is in the right place.

For your grid of values it depends on whether you want the values horizontal or vertical. If horizontal, your first row would be
1, 2, 3, 4, 5, 6, 7, 8. If vertical, it would be 1, 33, 65, 97, 139, etc.

At the moment you are using While loops. However, as you know how many rows and columns there are and as you are always going up
by 1, it would be better to use For loops.

Take out the counter loop and increment it manually inside tyour row and column loops.

That's all for now. Come back to me with the next version. :-)

Regards,
Fergus
 
Hi Richard,

You meany!! I was going to lead him there with carefully staged questions, lol.

Changing topic completely (if stephanie will let me [pokes out tongue]), did you come to any conclusions about creating your
multitude of classes and their properties?

Regards,
Fergus
 
256 = 11111111

Hi, that's incorrect. The number 256 = 100000000

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Hi, May I ask why 1 to 256? 1-255 are 8-bit numbers, but 256 is a 9-bit
number. Are you sure you don't want 0-255, there are still 256 numbers in
0-255.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Back
Top