Sum of bit differences for numbers from 0 to N
Here given code implementation process.
// C Program
// Sum of bit differences for numbers from 0 to N
#include <stdio.h>
#include <math.h>
// Returns the number of bit in given number
int countBits(int n)
{
if (n == 0)
{
return 0;
}
return (int)(log(n) / log(2) + 1);
}
int difference(int a, int b)
{
// Find the number of bits exist in given numbers
int n1 = countBits(a);
int n2 = countBits(b);
// Define counter resultant variable
int result = 0;
int i = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) & a) != ((1 << i) & b))
{
// When bit position of (1<<i) is not similar in given number
result++;
}
i--;
}
// Returns the result of bit difference
return result;
}
// Find the bits difference of given number from 0 to n
void findBitDifference(int n)
{
// Used to collecting of bits difference
int result = 0;
// Execute loop through by n
for (int i = 1; i <= n; ++i)
{
result += difference(i, i - 1);
}
// Display given n
printf("\n Given N : %d", n);
// Display calculated result
printf("\n Result : %d", result);
}
int main(int argc, char
const *argv[])
{
// Test Case
// n = 8
findBitDifference(8);
// n = 15
findBitDifference(15);
return 0;
}
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
/*
Java program
Sum of bit differences for numbers from 0 to N
*/
public class BitDifference
{
// Returns the number of bit in given number
public int countBits(int n)
{
if (n == 0)
{
return 0;
}
return (int)(Math.log(n) / Math.log(2) + 1);
}
public int difference(int a, int b)
{
// Find the number of bits exist in given numbers
int n1 = countBits(a);
int n2 = countBits(b);
// Define counter resultant variable
int result = 0;
int i = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) & a) != ((1 << i) & b))
{
// When bit position of (1<<i) is not similar in given number
result++;
}
i--;
}
// Returns the result of bit difference
return result;
}
// Find the bits difference of given number from 0 to n
public void findBitDifference(int n)
{
// Used to collecting of bits difference
int result = 0;
// Execute loop through by n
for (int i = 1; i <= n; ++i)
{
result += difference(i, i - 1);
}
// Display given n
System.out.print("\n Given N : " + n);
// Display calculated result
System.out.print("\n Result : " + result);
}
public static void main(String[] args)
{
BitDifference task = new BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
}
}
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
/*
C++ program
Sum of bit differences for numbers from 0 to N
*/
class BitDifference
{
public:
// Returns the number of bit in given number
int countBits(int n)
{
if (n == 0)
{
return 0;
}
return (int)(log(n) / log(2) + 1);
}
int difference(int a, int b)
{
// Returns the result of bit difference
// Find the number of bits exist in given numbers
int n1 = this->countBits(a);
int n2 = this->countBits(b);
// Define counter resultant variable
int result = 0;
int i = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) &a) != ((1 << i) &b))
{
// When bit position of (1<<i) is not similar in given number
result++;
}
i--;
}
return result;
}
// Find the bits difference of given number from 0 to n
void findBitDifference(int n)
{
// Used to collecting of bits difference
int result = 0;
// Execute loop through by n
for (int i = 1; i <= n; ++i)
{
result += this->difference(i, i - 1);
}
// Display given n
cout << "\n Given N : " << n;
// Display calculated result
cout << "\n Result : " << result;
}
};
int main()
{
BitDifference task = BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
return 0;
}
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
// Include namespace system
using System;
/*
C# program
Sum of bit differences for numbers from 0 to N
*/
public class BitDifference
{
// Returns the number of bit in given number
public int countBits(int n)
{
if (n == 0)
{
return 0;
}
return (int)(Math.Log(n) / Math.Log(2) + 1);
}
public int difference(int a, int b)
{
// Returns the result of bit difference
// Find the number of bits exist in given numbers
int n1 = countBits(a);
int n2 = countBits(b);
// Define counter resultant variable
int result = 0;
int i = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) & a) != ((1 << i) & b))
{
// When bit position of (1<<i) is not similar in given number
result++;
}
i--;
}
return result;
}
// Find the bits difference of given number from 0 to n
public void findBitDifference(int n)
{
// Used to collecting of bits difference
int result = 0;
// Execute loop through by n
for (int i = 1; i <= n; ++i)
{
result += difference(i, i - 1);
}
// Display given n
Console.Write("\n Given N : " + n);
// Display calculated result
Console.Write("\n Result : " + result);
}
public static void Main(String[] args)
{
BitDifference task = new BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
}
}
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
<?php
/*
Php program
Sum of bit differences for numbers from 0 to N
*/
class BitDifference
{
// Returns the number of bit in given number
public function countBits($n)
{
if ($n == 0)
{
return 0;
}
return (intval(log($n) / log(2)) + 1);
}
public function difference($a, $b)
{
// Returns the result of bit difference
// Find the number of bits exist in given numbers
$n1 = $this->countBits($a);
$n2 = $this->countBits($b);
// Define counter resultant variable
$result = 0;
$i = 0;
// Get length of higher bits
if ($n1 > $n2)
{
$i = $n1;
}
else
{
$i = $n2;
}
// Execute loop through by length of higher bit of given number
while ($i >= 0)
{
if (((1 << $i) & $a) != ((1 << $i) & $b))
{
// When bit position of (1<<i) is not similar in given number
$result++;
}
$i--;
}
return $result;
}
// Find the bits difference of given number from 0 to n
public function findBitDifference($n)
{
// Used to collecting of bits difference
$result = 0;
// Execute loop through by n
for ($i = 1; $i <= $n; ++$i)
{
$result += $this->difference($i, $i - 1);
}
// Display given n
echo "\n Given N : ". $n;
// Display calculated result
echo "\n Result : ". $result;
}
}
function main()
{
$task = new BitDifference();
// Test Case
// n = 8
$task->findBitDifference(8);
// n = 15
$task->findBitDifference(15);
}
main();
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
/*
Node Js program
Sum of bit differences for numbers from 0 to N
*/
class BitDifference
{
// Returns the number of bit in given number
countBits(n)
{
if (n == 0)
{
return 0;
}
return ((parseInt(Math.log(n) / Math.log(2)) + 1));
}
difference(a, b)
{
// Returns the result of bit difference
// Find the number of bits exist in given numbers
var n1 = this.countBits(a);
var n2 = this.countBits(b);
// Define counter resultant variable
var result = 0;
var i = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) & a) != ((1 << i) & b))
{
// When bit position of (1<<i) is not similar in given number
result++;
}
i--;
}
return result;
}
// Find the bits difference of given number from 0 to n
findBitDifference(n)
{
// Used to collecting of bits difference
var result = 0;
// Execute loop through by n
for (var i = 1; i <= n; ++i)
{
result += this.difference(i, i - 1);
}
// Display given n
process.stdout.write("\n Given N : " + n);
// Display calculated result
process.stdout.write("\n Result : " + result);
}
}
function main()
{
var task = new BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
}
main();
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
import math
# Python 3 program
# Sum of bit differences for numbers from 0 to N
class BitDifference :
# Returns the number of bit in given number
def countBits(self, n) :
if (n == 0) :
return 0
return int((int(math.log(n) / math.log(2)) + 1))
def difference(self, a, b) :
# Returns the result of bit difference
# Find the number of bits exist in given numbers
n1 = self.countBits(a)
n2 = self.countBits(b)
# Define counter resultant variable
result = 0
i = 0
# Get length of higher bits
if (n1 > n2) :
i = n1
else :
i = n2
# Execute loop through by length of higher bit of given number
while (i >= 0) :
if (((1 << i) & a) != ((1 << i) & b)) :
# When bit position of (1<<i) is not similar in given number
result += 1
i -= 1
return result
# Find the bits difference of given number from 0 to n
def findBitDifference(self, n) :
# Used to collecting of bits difference
result = 0
i = 1
# Execute loop through by n
while (i <= n) :
result += self.difference(i, i - 1)
i += 1
# Display given n
print("\n Given N : ", n, end = "")
# Display calculated result
print("\n Result : ", result, end = "")
def main() :
task = BitDifference()
# Test Case
# n = 8
task.findBitDifference(8)
# n = 15
task.findBitDifference(15)
if __name__ == "__main__": main()
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
# Ruby program
# Sum of bit differences for numbers from 0 to N
class BitDifference
# Returns the number of bit in given number
def countBits(n)
if (n == 0)
return 0
end
return ((Math.log(n) / Math.log(2) + 1)).to_i
end
def difference(a, b)
# Returns the result of bit difference
# Find the number of bits exist in given numbers
n1 = self.countBits(a)
n2 = self.countBits(b)
# Define counter resultant variable
result = 0
i = 0
# Get length of higher bits
if (n1 > n2)
i = n1
else
i = n2
end
# Execute loop through by length of higher bit of given number
while (i >= 0)
if (((1 << i) & a) != ((1 << i) & b))
# When bit position of (1<<i) is not similar in given number
result += 1
end
i -= 1
end
return result
end
# Find the bits difference of given number from 0 to n
def findBitDifference(n)
# Used to collecting of bits difference
result = 0
i = 1
# Execute loop through by n
while (i <= n)
result += self.difference(i, i - 1)
i += 1
end
# Display given n
print("\n Given N : ", n)
# Display calculated result
print("\n Result : ", result)
end
end
def main()
task = BitDifference.new()
# Test Case
# n = 8
task.findBitDifference(8)
# n = 15
task.findBitDifference(15)
end
main()
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
/*
Scala program
Sum of bit differences for numbers from 0 to N
*/
class BitDifference
{
// Returns the number of bit in given number
def countBits(n: Int): Int = {
if (n == 0)
{
return 0;
}
return (((Math.log(n) / Math.log(2)).toInt + 1));
}
def difference(a: Int, b: Int): Int = {
// Returns the result of bit difference
// Find the number of bits exist in given numbers
var n1: Int = this.countBits(a);
var n2: Int = this.countBits(b);
// Define counter resultant variable
var result: Int = 0;
var i: Int = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) & a) != ((1 << i) & b))
{
// When bit position of (1<<i) is not similar in given number
result += 1;
}
i -= 1;
}
return result;
}
// Find the bits difference of given number from 0 to n
def findBitDifference(n: Int): Unit = {
// Used to collecting of bits difference
var result: Int = 0;
var i: Int = 1;
// Execute loop through by n
while (i <= n)
{
result += this.difference(i, i - 1);
i += 1;
}
// Display given n
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitDifference = new BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
}
}
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
import Foundation
/*
Swift 4 program
Sum of bit differences for numbers from 0 to N
*/
class BitDifference
{
// Returns the number of bit in given number
func countBits(_ n: Int)->Int
{
if (n == 0)
{
return 0;
}
return Int(log(Double(n)) / log(2) + 1);
}
func difference(_ a: Int, _ b: Int)->Int
{
// Returns the result of bit difference
// Find the number of bits exist in given numbers
let n1: Int = self.countBits(a);
let n2: Int = self.countBits(b);
// Define counter resultant variable
var result: Int = 0;
var i: Int = 0;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
else
{
i = n2;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 << i) & a) != ((1 << i) & b))
{
// When bit position of (1<<i) is not similar in given number
result += 1;
}
i -= 1;
}
return result;
}
// Find the bits difference of given number from 0 to n
func findBitDifference(_ n: Int)
{
// Used to collecting of bits difference
var result: Int = 0;
var i: Int = 1;
// Execute loop through by n
while (i <= n)
{
result += self.difference(i, i - 1);
i += 1;
}
// Display given n
print("\n Given N : ", n, terminator: "");
// Display calculated result
print("\n Result : ", result, terminator: "");
}
}
func main()
{
let task: BitDifference = BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
}
main();
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
/*
Kotlin program
Sum of bit differences for numbers from 0 to N
*/
class BitDifference
{
// Returns the number of bit in given number
fun countBits(n: Int): Int
{
if (n == 0)
{
return 0;
}
return (Math.log(n.toDouble()) / Math.log(2.toDouble()) + 1).toInt();
}
fun difference(a: Int, b: Int): Int
{
// Returns the result of bit difference
// Find the number of bits exist in given numbers
var n1: Int = this.countBits(a);
var n2: Int = this.countBits(b);
// Define counter resultant variable
var result: Int = 0;
var i: Int = n2;
// Get length of higher bits
if (n1 > n2)
{
i = n1;
}
// Execute loop through by length of higher bit of given number
while (i >= 0)
{
if (((1 shl i) and a) != ((1 shl i) and b))
{
// When bit position of (1<<i) is not similar in given number
result += 1;
}
i -= 1;
}
return result;
}
// Find the bits difference of given number from 0 to n
fun findBitDifference(n: Int): Unit
{
// Used to collecting of bits difference
var result: Int = 0;
var i: Int = 1;
// Execute loop through by n
while (i <= n)
{
result += this.difference(i, i - 1);
i += 1;
}
// Display given n
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
var task: BitDifference = BitDifference();
// Test Case
// n = 8
task.findBitDifference(8);
// n = 15
task.findBitDifference(15);
}
Output
Given N : 8
Result : 15
Given N : 15
Result : 26
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment