Sum of n numbers with exactly 2 active bits set
Here given code implementation process.
// C Program for
// Sum of n numbers with exactly 2 active bits set
#include <stdio.h>
void twoActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
printf(" Given n : %d\n", n);
// Display calculate sum
printf(" Result : %d\n", sum);
}
int main(int argc, char
const *argv[])
{
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
twoActiveBits(30);
return 0;
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
/*
Java Program
Sum of n numbers with exactly 2 active bits set
*/
public class BinaryBits
{
public void twoActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
System.out.println(" Given n : " + n);
// Display calculate sum
System.out.println(" Result : " + sum);
}
public static void main(String[] args)
{
BinaryBits task = new BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30);
}
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Sum of n numbers with exactly 2 active bits set
*/
class BinaryBits
{
public: void twoActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
cout << " Given n : " << n << endl;
// Display calculate sum
cout << " Result : " << sum << endl;
}
};
int main()
{
BinaryBits *task = new BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task->twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task->twoActiveBits(30);
return 0;
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
// Include namespace system
using System;
/*
Csharp Program
Sum of n numbers with exactly 2 active bits set
*/
public class BinaryBits
{
public void twoActiveBits(int n)
{
int count = n;
int temp = 1;
int num = 1;
int sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
Console.WriteLine(" Given n : " + n);
// Display calculate sum
Console.WriteLine(" Result : " + sum);
}
public static void Main(String[] args)
{
BinaryBits task = new BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30);
}
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
package main
import "fmt"
/*
Go Program
Sum of n numbers with exactly 2 active bits set
*/
type BinaryBits struct {}
func getBinaryBits() * BinaryBits {
var me *BinaryBits = &BinaryBits {}
return me
}
func(this BinaryBits) twoActiveBits(n int) {
var count int = n
var temp int = 1
var num int = 1
var sum int = 0
for (count > 0) {
num = (num << 1)
for (temp < num && count > 0) {
sum += (num | temp)
temp = temp << 1
count--
}
temp = 1
}
// Display number of element
fmt.Println(" Given n : ", n)
// Display calculate sum
fmt.Println(" Result : ", sum)
}
func main() {
var task * BinaryBits = getBinaryBits()
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10)
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30)
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
<?php
/*
Php Program
Sum of n numbers with exactly 2 active bits set
*/
class BinaryBits
{
public function twoActiveBits($n)
{
$count = $n;
$temp = 1;
$num = 1;
$sum = 0;
while ($count > 0)
{
$num = ($num << 1);
while ($temp < $num && $count > 0)
{
$sum += ($num | $temp);
$temp = $temp << 1;
$count--;
}
$temp = 1;
}
// Display number of element
echo(" Given n : ".$n.
"\n");
// Display calculate sum
echo(" Result : ".$sum.
"\n");
}
}
function main()
{
$task = new BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
$task->twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
$task->twoActiveBits(30);
}
main();
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
/*
Node JS Program
Sum of n numbers with exactly 2 active bits set
*/
class BinaryBits
{
twoActiveBits(n)
{
var count = n;
var temp = 1;
var num = 1;
var sum = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count--;
}
temp = 1;
}
// Display number of element
console.log(" Given n : " + n);
// Display calculate sum
console.log(" Result : " + sum);
}
}
function main()
{
var task = new BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30);
}
main();
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
# Python 3 Program
# Sum of n numbers with exactly 2 active bits set
class BinaryBits :
def twoActiveBits(self, n) :
count = n
temp = 1
num = 1
sum = 0
while (count > 0) :
num = (num << 1)
while (temp < num and count > 0) :
sum += (num | temp)
temp = temp << 1
count -= 1
temp = 1
# Display number of element
print(" Given n : ", n)
# Display calculate sum
print(" Result : ", sum)
def main() :
task = BinaryBits()
# Test A
# n = 10
# --------------
# Binary
# 3 11
# 5 101
# 6 110
# 9 1001
# 10 1010
# 12 1100
# 17 10001
# 18 10010
# 20 10100
# 24 11000
# [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
# ------------------------------------------------
# Sum : 124
task.twoActiveBits(10)
# Test B
# num = 30
# --------------
# Binary
# 3 11
# 5 101
# 6 110
# 9 1001
# 10 1010
# 12 1100
# 17 10001
# 18 10010
# 20 10100
# 24 11000
# 33 100001
# 34 100010
# 36 100100
# 40 101000
# 48 110000
# 65 1000001
# 66 1000010
# 68 1000100
# 72 1001000
# 80 1010000
# 96 1100000
# 129 10000001
# 130 10000010
# 132 10000100
# 136 10001000
# 144 10010000
# 160 10100000
# 192 11000000
# 257 100000001
# 258 100000010
# [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
# 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
# 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
# ---------------------------------------------------
# Sum : 2300
task.twoActiveBits(30)
if __name__ == "__main__": main()
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
# Ruby Program
# Sum of n numbers with exactly 2 active bits set
class BinaryBits
def twoActiveBits(n)
count = n
temp = 1
num = 1
sum = 0
while (count > 0)
num = (num << 1)
while (temp < num && count > 0)
sum += (num | temp)
temp = temp << 1
count -= 1
end
temp = 1
end
# Display number of element
print(" Given n : ", n, "\n")
# Display calculate sum
print(" Result : ", sum, "\n")
end
end
def main()
task = BinaryBits.new()
# Test A
# n = 10
# --------------
# Binary
# 3 11
# 5 101
# 6 110
# 9 1001
# 10 1010
# 12 1100
# 17 10001
# 18 10010
# 20 10100
# 24 11000
# [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
# ------------------------------------------------
# Sum : 124
task.twoActiveBits(10)
# Test B
# num = 30
# --------------
# Binary
# 3 11
# 5 101
# 6 110
# 9 1001
# 10 1010
# 12 1100
# 17 10001
# 18 10010
# 20 10100
# 24 11000
# 33 100001
# 34 100010
# 36 100100
# 40 101000
# 48 110000
# 65 1000001
# 66 1000010
# 68 1000100
# 72 1001000
# 80 1010000
# 96 1100000
# 129 10000001
# 130 10000010
# 132 10000100
# 136 10001000
# 144 10010000
# 160 10100000
# 192 11000000
# 257 100000001
# 258 100000010
# [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
# 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
# 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
# ---------------------------------------------------
# Sum : 2300
task.twoActiveBits(30)
end
main()
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
/*
Scala Program
Sum of n numbers with exactly 2 active bits set
*/
class BinaryBits()
{
def twoActiveBits(n: Int): Unit = {
var count: Int = n;
var temp: Int = 1;
var num: Int = 1;
var sum: Int = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count -= 1;
}
temp = 1;
}
// Display number of element
println(" Given n : " + n);
// Display calculate sum
println(" Result : " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BinaryBits = new BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30);
}
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
/*
Swift 4 Program
Sum of n numbers with exactly 2 active bits set
*/
class BinaryBits
{
func twoActiveBits(_ n: Int)
{
var count: Int = n;
var temp: Int = 1;
var num: Int = 1;
var sum: Int = 0;
while (count > 0)
{
num = (num << 1);
while (temp < num && count > 0)
{
sum += (num | temp);
temp = temp << 1;
count -= 1;
}
temp = 1;
}
// Display number of element
print(" Given n : ", n);
// Display calculate sum
print(" Result : ", sum);
}
}
func main()
{
let task: BinaryBits = BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30);
}
main();
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
/*
Kotlin Program
Sum of n numbers with exactly 2 active bits set
*/
class BinaryBits
{
fun twoActiveBits(n: Int): Unit
{
var count: Int = n;
var temp: Int = 1;
var num: Int = 1;
var sum: Int = 0;
while (count > 0)
{
num = (num shl 1);
while (temp < num && count > 0)
{
sum += (num or temp);
temp = temp shl 1;
count -= 1;
}
temp = 1;
}
// Display number of element
println(" Given n : " + n);
// Display calculate sum
println(" Result : " + sum);
}
}
fun main(args: Array < String > ): Unit
{
val task: BinaryBits = BinaryBits();
// Test A
// n = 10
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24]
// ------------------------------------------------
// Sum : 124
task.twoActiveBits(10);
// Test B
// num = 30
// --------------
// Binary
// 3 11
// 5 101
// 6 110
// 9 1001
// 10 1010
// 12 1100
// 17 10001
// 18 10010
// 20 10100
// 24 11000
// 33 100001
// 34 100010
// 36 100100
// 40 101000
// 48 110000
// 65 1000001
// 66 1000010
// 68 1000100
// 72 1001000
// 80 1010000
// 96 1100000
// 129 10000001
// 130 10000010
// 132 10000100
// 136 10001000
// 144 10010000
// 160 10100000
// 192 11000000
// 257 100000001
// 258 100000010
// [3 + 5 + 6 + 9 + 10 + 12 + 17 + 18 + 20 + 24 + 33 +
// 34 + 36 + 40 + 48 + 65 + 66 + 68 + 72 + 80 + 96 +
// 129 + 130 + 132 + 136 + 144 + 160 + 192 + 257 + 258]
// ---------------------------------------------------
// Sum : 2300
task.twoActiveBits(30);
}
Output
Given n : 10
Result : 124
Given n : 30
Result : 2300
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