// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; class MainClass { public static string ToBin1(byte b) { return (((b & 128) != 0) ? "1" : "0") + (((b & 64) != 0) ? "1" : "0") + (((b & 32) != 0) ? "1" : "0") + (((b & 16) != 0) ? "1" : "0") + (((b & 8) != 0) ? "1" : "0") + (((b & 4) != 0) ? "1" : "0") + (((b & 2) != 0) ? "1" : "0") + (((b & 1) != 0) ? "1" : "0"); } public static string ToBin2(byte b) { string res = ""; int tmp = b; for(int i = 0; i < 8; i++) { res = tmp % 2 + res; tmp /= 2; } return res; } public static void Main(string[] args) { byte by = 4; Console.WriteLine(ToBin1(by)); Console.WriteLine(ToBin2(by)); } }