Skip to content

Instantly share code, notes, and snippets.

@suterma
Last active May 29, 2019 20:53
Show Gist options
  • Save suterma/bdf3ac8d93f6279834875eff3d495af3 to your computer and use it in GitHub Desktop.
Save suterma/bdf3ac8d93f6279834875eff3d495af3 to your computer and use it in GitHub Desktop.
Try.NET showing a binary representation of integer number from the decimals system
using System;
using System.Linq;
using System.Collections.Generic;
namespace BinaryNumbersDisplay
{
public class Program
{
public static void Main()
{
int digitsDisplayCount = 5; //The number of digits to display
IEnumerable<int> numberRange = Enumerable.Range(0, 31); //The range of values to display
//Write the binary representation for each value in the range
foreach (int number in numberRange)
{
WriteDigits(digitsDisplayCount, number);
}
}
///<summary>Writes a set of digits, that represent the integer number in the binary system. </summary>
private static void WriteDigits(int digitCount, int number)
{
for (int i = digitCount - 1; i >= 0; i--)
{
var digitFilter = (int)Math.Pow(2, i);
bool isDigitSet = (number & digitFilter) > 0;
Console.Write(isDigitSet ? "█ " : "▁ ");
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment