Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Binary Literals and Digit Separators in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C# is a simple, high-level, general-purpose language. It supports the Object-oriented programming paradigm.  Microsoft developed it under the .NET platform. It is much similar to C/C++. C# can be used for web development, web services, and desktop application development.

Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.

Binary Literals

Literals are the constant values assigned to variables or can be used directly in C#. There are various types of literal defining the different sets of constant values. In the earlier versions of C# below C# 7.0, there were 6 types available in C#. Integer, floating-point, boolean, character, string, and NULL are the six literals present in C# below 7.0. From C# 7.0 and above, one more literal was added, called binary literal. Binary literal are the constant values in binary format, 0 or 1.

Before this, only decimal and hexadecimal values could be stored in C#, but with the addition of binary literals, it became possible for us to store the binary values. To specify a value to be binary, we have to give it 0b as a prefix. Below is an example of how you can store the binary value of 10 in a variable.

var temp = 0b1010;


Here, you can see that we have used the 0b prefix to specify that the literal is binary and has to be stored as a binary literal in variable temp. 

Note: If the same statement is written in earlier versions of C# that is below 7.0, then it will raise an error as it is introduced after C# 7.0. 

We can use different built-in functions to typecast the binary literals to other data types. For example, we can use ToChar or ToInt32 to convert the binary literals to character or integer data types. By default, the integer value is printed when we output/print the binary literal.

C# implementation to demonstrate Binary Literals  

/* Program to demonstrate the use of binary literals in C# */
using System;

class Ninjas{
 
    /* Driver Function */
    static public void Main()
    {
        /* Specifying binary literals with 0b and storing the values in variables a and b */
        var a = 0b10001; /* a has the integer value of 17 */
        var b = 0b01101110;  /*b has the binary value of n */
 
        /* Printing values to demonstrate binary literals and their conversion to other types */
        Console.WriteLine("Binary literal stored at a is: " + a);
        Console.WriteLine("Binary literal stored at b is:  " + b);
        Console.WriteLine("Integer Value of a is: {0}", Convert.ToInt32(a));
        Console.WriteLine("Character Value of b is: {0}", Convert.ToChar(b));
    }
}

Output

Binary literal stored at a is: 17
Binary literal stored at b is: 110
Integer Value of a is: 17
Character Value of b is: n
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Digit Separators

Like the binary literals, digit separators are also introduced in C# 7.0, so they can not be used in the lower versions of C#. Digit separators make our code more readable by separating the large numbers. You can separate the large numbers using the Underscore (_) symbol. It works with integers, binary literals, and hexadecimal numbers. The compiler simply ignores the Underscore (_) symbol. So when we output/print the numbers on the screen, the underscores (_) are not displayed. You can place any number of underscores.

Note: You cannot use Underscore(_) digit separator after the prefix 0b and 0x for binary and hexadecimal literals. Doing so will raise errors. 

C# implementation to demonstrate digit separators

/* Program to demonstrate digit separators in C# */
using System;
 
class Ninjas {

    /* Driver function */
    static public void Main()
    {
        /* Storing same value in different variables using different number of digit separators */
        long Num1 = 100_001_123_321_001;
        long Num2 = 1_00_001_12_3321_0_01;
        long Num3 = 100001123321_001;
 
        Console.WriteLine("Num1: {0}", Num1);
        Console.WriteLine("Num2: {0}", Num2);
        Console.WriteLine("Num3: {0}", Num3);
       
        /* Using digit separators with binary and hexadecimal literals */
        var Binary_literal = 0b11_100_10_100;
        var Hexa_literal = 0x63_B2;
       
        Console.WriteLine("Binary_literal: {0}", Binary_literal);
        Console.WriteLine("Hexa_literal: {0}", Hexa_literal);
    }
}

Output

Num1: 100001123321001
Num2: 100001123321001
Num3: 100001123321001
Binary_literal: 916
Hexa_literal: 25522

 

Must read decimal to binary c++, singleton design pattern in c# 

FAQs

What is C#?

C# is a simple, high-level, general-purpose language. It supports the Object-oriented programming paradigm. Microsoft developed it under the .NET platform.

What is the prefix used to specify a number as a binary literal?

The 0b prefix specifies a number as a binary literal.

What is the prefix used to specify a number as a hexadecimal literal?

The 0x prefix is used to specify a number as a binary literal.

How many underscores can be used as a digit separator in any number?

You can use any number of digit separators in any number. The number can be an integer, binary, or hexadecimal literal.

What do the digit separators do? How are they interpreted by the compiler?

The digit separators improve the readability of a C# program by providing a method to separate a large number. It is ignored by the compiler and is not printed when we display the numbers.

Conclusion

In this article, we discussed Binary literals in C#. We also discussed the digit separators in C#. To learn C# visit our site C# | Learn and Practice. I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Topics covered
1.
Introduction
2.
Binary Literals
2.1.
C# implementation to demonstrate Binary Literals  
2.1.1.
Output
3.
Digit Separators
3.1.
C# implementation to demonstrate digit separators
3.1.1.
Output
4.
FAQs
4.1.
What is C#?
4.2.
What is the prefix used to specify a number as a binary literal?
4.3.
What is the prefix used to specify a number as a hexadecimal literal?
4.4.
How many underscores can be used as a digit separator in any number?
4.5.
What do the digit separators do? How are they interpreted by the compiler?
5.
Conclusion