Tag Archives: binary

C# || Counting Bits – How To Return The Number Of 1’s In Binary Representation Of X Using C#

The following is a module with functions which demonstrates how to return the number of 1’s in binary representation of X using C#.


1. Count Bits – Problem Statement

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1‘s in the binary representation of i.

Example 1:


Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10

Example 2:


Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101


2. Count Bits – Solution

The following is a solution which demonstrates how return the number of 1’s in binary representation of X.

In this problem, we can see a pattern start to form.

When the current n is even, we can get the answer by dividing by 2 (e.g result[n] = result[n / 2])

When the current n is odd, we can get the answer by getting the result at previous index and adding 1 (e.g result[n] = result[n – 1] + 1)

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[0,1,1]
[0,1,1,2,1,2]

C# || How To Add Two Binary Strings Using C#

The following is a module with functions which demonstrates how to add two binary strings together using C#.


1. Add Binary – Problem Statement

Given two binary strings a and b, return their sum as a binary string.

Example 1:


Input: a = "11", b = "1"
Output: "100"

Example 2:


Input: a = "1010", b = "1011"
Output: "10101"


2. Add Binary – Solution

The following is a solution which demonstrates how to add two binary strings together.

In this solution, we start at the end of both strings, and perform basic math on each number, adding them together. If any mathematical carry over is required, that is added to the next loop iteration.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


"100"
"10101"

C++ || Snippet – How To Convert A Decimal Number Into Binary

This page will demonstrate how to convert a decimal number (i.e a whole number) into its binary equivalent. So for example, if the decimal number of 26 was entered into the program, it would display the converted binary value of 11010.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

How To Count In Binary
The "Long" Datatype - What Is It?
While Loops
Online Binary to Decimal Converter - Verify For Correct Results
How To Reverse A String

If you are looking for sample code which converts binary to decimal, check back here soon!


The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
Note: The code was compiled 3 separate times to display different output

====== RUN 1 ======

Please enter an integer value: 1987

The integer value of 1987 = 11111000011 in binary

====== RUN 2 ======

Please enter an integer value: -26

The integer value of -26 = -11010 in binary

====== RUN 3 ======

Please enter an integer value: 12345678910

The integer value of 12345678910 = 1011011111110111000001110000111110 in binary

Java || Snippet – How To Convert A Decimal Number Into Binary

This page will demonstrate how to convert a decimal number (i.e a whole number) into its binary equivalent. So for example, if the decimal number of 25 was entered into the program, it would display the converted binary value of 11001.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

How To Count In Binary
The "Long" Datatype - What Is It?
Methods (A.K.A "Functions") - What Are They?
While Loops
Online Binary to Decimal Converter - Verify For Correct Results

If you are looking for sample code which converts binary to decimal, check back here soon!


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
Note: The code was compiled 3 separate times to display different output

====== RUN 1 ======

Welcome to My Programming Notes' Java Program.

Please enter an integer value: 5

The integer value of 5 = 101 in binary

====== RUN 2 ======

Welcome to My Programming Notes' Java Program.

Please enter an integer value: -25

The integer value of -25 = -11001 in binary

====== RUN 3 ======

Welcome to My Programming Notes' Java Program.

Please enter an integer value: 12345678910

The integer value of 12345678910 = 1011011111110111000001110000111110 in binary