## TheDeveloperBlog.com

<< Back to C-SHARP

# C# Binary Representation int (Convert, toBase 2)

Print binary representations for ints. Display bits as 1 and 0.
Binary representation. Ints have 32 bits. We want to see which ones are turned on. We want to see the sign bit. A simple method can be used to display bits.
Some notes. The binary representation method here has some performance improvements and a simpler calling pattern. Meanwhile Convert.ToString with PadLeft is the shortest solution.
Example. Here we look a method that can display bits as zeros and ones from an integer. Every 32-bit integer can be converted into a text display of 32 zeros and ones with this method.

Note: This implementation is not as short as possible, but it helps illustrate how to print bits.

Note 2: Please see the Convert toBase 2 example for a shorter (and easier to maintain) implementation.

AndShift
C# program that shows binary representation using System; class Program { static void Main() { // Write full binary string for 100. Console.WriteLine(GetIntBinaryString(100)); // Write full binary string for 100000. Console.WriteLine(GetIntBinaryString(100000)); } static string GetIntBinaryString(int n) { char[] b = new char; int pos = 31; int i = 0; while (i < 32) { if ((n & (1 << i)) != 0) { b[pos] = '1'; } else { b[pos] = '0'; } pos--; i++; } return new string(b); } } Output 00000000000000000000000001100100 00000000000000011000011010100000 Notes, above method. The method receives a normal integer, and then loops over each of the 32 bit positions. At the same time, it writes zeros and ones in reverse order.

Results: The output appears to be correct. To test, I used an online tool from the City University of New York.

Decimal, Binary Conversion Tool: cuny.edu
Convert, toBase. Consider this implementation of GetIntBinaryString. We use Convert.ToString and specify the toBase argument as 2. A binary representation is base 2.

Also: We use PadLeft to ensure the string has 32 chars total, with padding added to the left (the start).

C# program that uses Convert.ToString, base 2 using System; class Program { static void Main() { Console.WriteLine(GetIntBinaryString(100)); Console.WriteLine(GetIntBinaryString(100000)); } static string GetIntBinaryString(int value) { // Use Convert class and PadLeft. return Convert.ToString(value, 2).PadLeft(32, '0'); } } Output 00000000000000000000000001100100 00000000000000011000011010100000 TrimStart example. This method calls TrimStart. It provides output identical to the JavaScript tool. I haven't found this method as useful, because usually the trailing zeros are useful.TrimEnd, TrimStart
Method that uses TrimStart: C# static string GetIntBinaryStringRemoveZeros(int n) { char[] b = new char; int pos = 31; int i = 0; while (i < 32) { if ((n & (1 << i)) != 0) { b[pos] = '1'; } else { b[pos] = '0'; } pos--; i++; } return new string(b).TrimStart('0'); } Summary. We displayed 1s and 0s from an integer, using the C# language. This sort of debugging code can solve hard problems when you are working on complicated structures.

Note: Thanks to Markus Burrer for providing the Convert.ToString with toBase 2. This is a simpler solution.

A review. The functions here clearly display the leftmost bit as the sign. They use character arrays which enhance performance. The ToString method is the simplest and shortest.