Open In App

Binary Literals and Digit Separators in C#

Last Updated : 30 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Binary Literals

The fixed values are called as Literal. Literal is a value which is used by the variables. Before C# 7.0 six types of literals are available that are an integer, Floating-point, Character, String, Null, Boolean literals. In C# 7.0 there is one more literal is added which is known as Binary Literal. The binary literal is used to store the binary value in a variable. And the binary literal is represented by 0b. Binary literals are mainly used for bitmasks.

Example:

var num = 0b10001

Here when the compiler sees 0b in the variable value, then it automatically treated this num as a binary literal. If you try to run this on the compilers below C# 7.0, then the compiler will throw an error because this feature is introduced in C# 7.0 so, it will only work on C# 7.0 and above compilers.

Example:




// C# program to illustrate the 
// concept of binary literals.
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating binary literals 
        // by prefixing with 0b
        var num1 = 0b1001;
        var num2 = 0b01000011;
  
        Console.WriteLine("Value of Num1 is: " + num1);
        Console.WriteLine("Value of Num2 is: " + num2);
        Console.WriteLine("Char value of Num2 is: {0}",
                                 Convert.ToChar(num2));
    }
}


Output:

Value of Num1 is: 9
Value of Num2 is: 67
Char value of Num2 is: C

Digit Separator

The concept of digit separator is introduced in C# 7.0. With the help of digit separator, you can separate the large number into small parts which makes your code more readable. Underscore(_) is used as a digit separator. When you use digit separator in your code they are simply ignored by the compiler, so the compiler does not print digit separators in the output like as shown in the below example.

Example:




// C# program to illustrate the 
// concept of digit separators.
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Without Using digit separators
        long x = 100000022200000202;
        long z = 10000000020;
  
        Console.WriteLine("X: {0}", x);
        Console.WriteLine("Z: {0}", z);
  
        // Using digit separators
        long num1 = 1_00_10_00_00_00;
        var num2 = 0b_010_000_000_000_000_000_000_000_000;
        long num3 = 1_00_00_00_00_00_00;
        var num4 = 0b_1_1000_0000_1000_0000_0011_0000_0000_1000_0001;
  
        Console.WriteLine("Num1: {0}", num1);
        Console.WriteLine("Num2: {0}", num2);
        Console.WriteLine("Num3: {0}", num3);
        Console.WriteLine("Num4: {0}", num4);
    }
}


Output:

X: 100000022200000202
Z: 10000000020
Num1: 1000110000000000
Num2: 33554432
Num3: 1000000000000000000
Num4: 103213629569


Similar Reads

C# | Literals
The fixed values are called as Literal. Literal is a value that is used by the variables. Values can be either an integer, float or string, etc. // Here 100 is a constant/literal.int x = 100; Literals can be of the following types: Integer LiteralsFloating-point LiteralsCharacter LiteralsString LiteralsNull LiteralsBoolean LiteralsInteger Literals:
5 min read
C# Binary Writer
Pre-requisites: Introduction to C# C# is a general-purpose, modern and object-oriented programming language pronounced as “C Sharp”. It was developed by Microsoft led by Anders Hejlsberg. In C# Binary Writer is a class that is used to write primitive types as binary data in particular encoding stream. It is present under the System.IO namespace. pu
3 min read
C# Program to Convert a Binary String to an Integer
Given an binary string as input, we need to write a program to convert the binary string into equivalent integer. To convert an binary string to integer, we have to use Convert.ToInt32(String, Base/Int32) function to convert the values. The base of the binary is 2. Syntax: Convert.ToInt32(String, Base/Int32); Examples: Input : 1010101010101010 Outp
2 min read
C# Program to Find Binary Equivalent of an Integer using Recursion
Given an integer number, now we convert the given integer number into a binary number using recursion. Recursion is a method in which a function calls itself directly or indirectly and such type of function is known as a recursive function. It solves the problem very efficiently like we find the binary equivalent of an integer. Examples: Input : 10
3 min read
Implementing Binary Reader Using C#
BinaryReader is a class that is present under the System.IO namespace. This is used for handling the binary values from a particular encoding stream. Syntax: This will create the BinaryReader object for a particular input stream by using UTF-8 encoding. BinaryReader br = new BinaryReader(Stream) This will take a particular input stream and can pass
2 min read
C# Program to Overload Unary Increment (++) and Decrement (--) Operators
In C#, overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement method overloading by defining two or more methods in a class sharing the same name but with different method signatures. So in this article, we will learn how to overload unary increment and decrem
3 min read
How to Install and Setup Visual Studio for ASP.NET?
Visual Studio is an Integrated Development Environment(IDE) developed by Microsoft to develop GUI(Graphical User Interface), Web applications, console, web apps, mobile apps, cloud, and web services, etc. To install and use Visual Studio for the commercial purpose one must buy a license from Microsoft. For learning (non-commercial) purpose, Microso
4 min read
C# | Boxing And Unboxing
Prerequisite : Data Types in C# Boxing and unboxing are important concepts in C#. The C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, Boxing converts a Value Type variable into a Reference Type variable, and Unboxing achieves the vice-versa. Boxing and Unboxing enable a
3 min read
Difference between SortedList and SortedDictionary in C#
In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection. The generic SortedList is defined in System.Collections.Generic namespace whereas non-generic SortedList is defined under System.C
3 min read
C# | TrimStart() and TrimEnd() Method
Prerequisite: Trim() Method in C# In C#, TrimStart() & TrimEnd() are the string methods. TrimStart() method is used to remove the occurrences of a set of characters specified in an array from the starting of the current String object. TrimEnd() method is used to remove the occurrences of a set of characters specified in an array from the ending
3 min read
Article Tags :