Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
9m 43s
Binary numbers are an essential part of computing, and understanding how to work with them in C# is crucial for handling low-level operations, bitwise manipulations, and efficient storage. In this article, we'll break down the key concepts of storing and displaying binary values in C#, following Tim Corey's video, Binary in C#: Storing and Displaying Binary Values in 10 minutes or less.
Tim explains this topic in a concise yet detailed manner, making it easier to grasp. Let's go step by step through his explanation and explore how binary values work in C#.
Tim begins by addressing the core question: how do we store a binary number in C#? He explains that binary numbers, such as 0b101, represent base-2 values and are stored in integer variables.
To store a binary value, Tim demonstrates the following syntax:
int binaryValue = 0b101; // Represents the decimal number 5Dim binaryValue As Integer = &B101 ' Represents the decimal number 5Here, 0b is a prefix that tells C# to interpret the following digits as a binary number. Tim highlights that 0b101 is equivalent to 5 in decimal notation.
He also notes that adding leading zeros (e.g., 0b00000101) does not change the value. The system still interprets it as 5 because those extra zeros hold no numerical significance.
Once we store a binary value, the next step is to display it. Tim points out an interesting behavior: when you print a binary-stored value, C# converts it to decimal by default.
For example:
Console.WriteLine($"Binary value is: {binaryValue}");
// Outputs: Binary value is: 5Console.WriteLine($"Binary value is: {binaryValue}")
' Outputs: Binary value is: 5Even though binaryValue was stored as 0b101, the console will output 5. Tim explains that C# works with base-10 by default when displaying numeric values.
To see the value in its binary format, we need a conversion.
Tim introduces a simple way to format an integer as a binary string:
string binaryString = Convert.ToString(binaryValue, 2);
Console.WriteLine($"Binary representation: {binaryString}");
// Outputs: Binary representation: 101Dim binaryString As String = Convert.ToString(binaryValue, 2)
Console.WriteLine($"Binary representation: {binaryString}")
' Outputs: Binary representation: 101He explains that Convert.ToString(value, 2) converts an integer to its binary representation as a string. This way, if you stored 0b101, the console will now correctly display 101 instead of 5.
Tim then tackles an issue that often arises when displaying binary numbers: formatting them consistently with leading zeros. In many cases, we want a binary number to appear in an 8-bit format (e.g., 00000101 instead of just 101).
He explains how to achieve this using PadLeft:
string formattedBinary = binaryString.PadLeft(8, '0');
Console.WriteLine($"Formatted binary: {formattedBinary}");
// Outputs: Formatted binary: 00000101Dim formattedBinary As String = binaryString.PadLeft(8, "0"c)
Console.WriteLine($"Formatted binary: {formattedBinary}")
' Outputs: Formatted binary: 00000101Here, PadLeft(8, '0') ensures the binary string always has at least 8 characters, filling in any missing spaces with zeros. This is especially useful when dealing with byte-sized binary values.
Tim clarifies that the single quotes around '0' are required because PadLeft works with characters, not strings.
Another important point Tim makes is that you don't have to input numbers in binary format (0b notation) to get their binary representation.
For instance, you can take a standard decimal number and convert it to binary:
int decimalNumber = 12;
string binaryRepresentation = Convert.ToString(decimalNumber, 2);
Console.WriteLine($"Binary equivalent of {decimalNumber} is {binaryRepresentation}");
// Outputs: Binary equivalent of 12 is 1100Dim decimalNumber As Integer = 12
Dim binaryRepresentation As String = Convert.ToString(decimalNumber, 2)
Console.WriteLine($"Binary equivalent of {decimalNumber} is {binaryRepresentation}")
' Outputs: Binary equivalent of 12 is 1100Here, the number 12 is stored as a decimal integer, but when converted, it correctly outputs 1100 in binary.
Tim emphasizes that this technique is useful for debugging and understanding how numbers are stored at the bit level.
Tim also touches on how binary values can be stored in different numeric data types. While integers (int) are the most common, C# allows storing binary values in other types like uint (unsigned integer) or long.
For example:
uint unsignedBinary = 0b1010; // 10 in decimal
long largeBinary = 0b1100110011; // A longer binary numberDim unsignedBinary As UInteger = &B1010 ' 10 in decimal
Dim largeBinary As Long = &B1100110011 ' A longer binary numberThe key difference with uint is that it does not support negative values, while long can hold much larger numbers.
Tim notes that understanding these distinctions helps when working with low-level operations like bitwise shifts and masking.
Tim concludes by summarizing the key takeaways:
0b prefix.Convert.ToString(value, 2) helps convert numbers into binary format.PadLeft ensures a consistent bit-length representation.With these techniques, working with binary values in C# becomes much easier and more intuitive. To get a better understanding of the topic, do watch his full video.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy