Binary transformation

301 Views Asked by At

The best way to explain this is with examples.

Insert a 0 at end: Multiply by 2

1010 -> (*2) -> 10100

Insert a 1 at end: Multiply by 2 and add 1

1010 -> (*2+1) -> 10101

Insert a 0 at start: Do nothing

1010 -> (*1) -> 1010 == 01010

What is the solution for insert a 1 at start?

Note: Add 2^4 is not a valid answer as it only works with 4 bits while the other 3 methods work on all bit lengths. Use big endian.

Edit: Just to make it double clear. The answer needs to be static like the other 3 methods. Checking the number of digits it has and then adding 2^n is NOT a valid answer. Assume we cannot count the number of digits.

1

There are 1 best solutions below

2
On

If your starting binary number has $n$ digits, adding 1 at the start means "add $2^n$ to the starting number"