C# Online Compiler
Example: C# Program to Convert Decimal to Binary Using Bitwise Operator
C
C++
C#
Java
Python
PHP
main.cs
STDIN
Run
// C# Program to Convert Decimal to Binary Using Bitwise Operator using System; class W3CW { // Main function static void Main() { // Step-1 Declare the input number int num = 10; // Step-2 Handle the zero case if (num == 0) { Console.WriteLine("Binary: 0"); return; } // Step-3 Build binary string using bitwise shift string binary = ""; for (int i = 31; i >= 0; i--) { int k = num >> i; // Check each bit if ((k & 1) == 1) binary += "1"; else if (binary.Length > 0) // skip leading zeros binary += "0"; } // Step-4 Print the binary output Console.WriteLine("Binary: " + binary); } }
Output
Clear
ADVERTISEMENTS