TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Binary Representation for Integer

This C# example prints binary representations. It displays bits as 1 and 0.

Binary representation. Integers have 32 bits.

We want to see which ones are turned on. We want to see the sign bit. The binary representation method here is special in a couple ways. It has some performance improvements and has a simpler calling pattern.

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. It internally uses a char array.

Char Array

Note: I feel the algorithm is simpler than many others. It may also be more efficient than those that use StringBuilder or string concats.

string.Concat

C# program that shows binary representation

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);
    }
}

Output

00000000000000000000000001100100
00000000000000011000011010100000

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. This results in standard results. The char array is the most efficient structure here.

Correctness: Methods that don't work are worse than useless. To test, I used an online tool from the City University of New York.

Note: The tool has browser JavaScript problems and you need to keep refreshing it to make sure it works.

Decimal, Binary Conversion Tool

Example 2. This next method simply calls TrimStart on the resulting string. It provides output identical to the CUNY JavaScript tool. I haven't found this method as useful, because usually the trailing zeros are useful.

TrimEnd, TrimStart

Method that uses TrimStart: C#

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');
}

Summary. We looked at how you can display the 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.

Review: The two functions here clearly display the leftmost bit as the sign. They use character arrays which enhance performance.

And Bitwise OperatorShift Operators


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf