Binary Representation int (Convert, toBase 2)Print binary representations for ints. Display bits as 1 and 0.
This page was last reviewed on Nov 17, 2022.
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.
Or, Bitwise
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.
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[32]; 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); } }
00000000000000000000000001100100 00000000000000011000011010100000
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).
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'); } }
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
static string GetIntBinaryStringRemoveZeros(int n) { char[] b = new char[32]; 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'); }
Notes, above methods. 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.
Result The output appears to be correct. To test, I used an online tool from the City University of New York.
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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).
© 2007-2023 Sam Allen.