Iron Academy Logo
Learn C#

Binary Values in C#: Storing and Displaying Binary Values

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#.

Storing Binary Values 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;
int binaryValue = 0b101;
TODO
$vbLabelText   $csharpLabel

Here, 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.

Printing Binary Values in C

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}");
Console.WriteLine($"Binary value is: {binaryValue}");
TODO
$vbLabelText   $csharpLabel

Even 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.

Converting Integers to Binary Format

Tim introduces a simple way to format an integer as a binary string:

string binaryString = Convert.ToString(binaryValue, 2);
Console.WriteLine($"Binary representation: {binaryString}");
string binaryString = Convert.ToString(binaryValue, 2);
Console.WriteLine($"Binary representation: {binaryString}");
TODO
$vbLabelText   $csharpLabel

He 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.

Padding Binary Values with Leading Zeros

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}");
string formattedBinary = binaryString.PadLeft(8, '0');
Console.WriteLine($"Formatted binary: {formattedBinary}");
TODO
$vbLabelText   $csharpLabel

Here, 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.

Binary Representation of Decimal Numbers

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}");
int decimalNumber = 12;
string binaryRepresentation = Convert.ToString(decimalNumber, 2);
Console.WriteLine($"Binary equivalent of {decimalNumber} is {binaryRepresentation}");
TODO
$vbLabelText   $csharpLabel

Here, 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.

Storing Binary Numbers in Different Data Types

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 number
uint unsignedBinary = 0b1010; // 10 in decimal
long largeBinary = 0b1100110011; // A longer binary number
TODO
$vbLabelText   $csharpLabel

The 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.

Final Thoughts

Tim concludes by summarizing the key takeaways:

  1. Binary numbers are stored in integer types using the 0b prefix.

  2. Printing an integer defaults to decimal representation, requiring explicit conversion for binary output.

  3. Convert.ToString(value, 2) helps convert numbers into binary format.

  4. PadLeft ensures a consistent bit-length representation.

  5. You can store binary numbers in different numeric types, depending on the use case.

With these techniques, working with binary values in C# becomes much easier and more intuitive. To get better understanding of the topic do watch his full video.

Hero Worlddot related to Binary Values in C#: Storing and Displaying Binary Values
Hero Affiliate related to Binary Values in C#: Storing and Displaying Binary Values

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!