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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started