Question: Given a value x, write x as a sum of all the powers of two of it's bits. Input Format A single positive whole number

Given a value x, write x as a sum of all the powers of two of it's bits.

Input Format

A single positive whole number

Constraints

Input will be a positive value less than 2^64. (it will fit inside a long data type)

Output Format

An arithmetic expression giving the value as the sum of it's powers of two.

Only display the powers of two that are required to sum to the value.

Include the original value on the left of an equals sign and the sum on the right.

for example given input value 11:

11 = 1*8 + 1*2 + 1*1 

Notice the value 4 is not included. Hint: You only need include powers of two when the matching bit of the value is 1.

Sample Input 0

7 

Sample Output 0

7 = 1*4 + 1*2 + 1*1 

Explanation 0

7 is 111 in binary. So we need the following powers of 2: 1, 2, and 4 We write our output as 7 = 1*4 + 1*2 + 1*1 Note where there are spaces. 

Sample Input 1

455 

Sample Output 1

455 = 1*256 + 1*128 + 1*64 + 1*4 + 1*2 + 1*1 

Explanation 1

Powers of 2 that equal 455 are 256, 128, 64, 4, 2, and 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!