// 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; using System.Text; namespace E { public class Program { public static short FromBin(string s) { short res = 0; char[] sa = s.ToCharArray(); for (int i = 0; i < 16; i++) { res = (short)((res << 1) | (sa[i] == '1' ? 1 : 0)); } return res; } public static string ToBin(short v) { string res = ""; short tmp = v; for (int i = 0; i < 16; i++) { res = (tmp & 1) + res; tmp = (short)(tmp >> 1); } return res; } public static void Main(string[] args) { short v = 77; string posstr = ToBin(v); short posnum = FromBin(posstr); Console.WriteLine(posstr + " " + posnum); v = (short)-v; string negstr = ToBin(v); short negnum = FromBin(negstr); Console.WriteLine(negstr + " " + negnum); } } }