New answers tagged

1

This depends on the rules of your programming language, which you need to check. For any fixed size integral type that has more than the single value zero, there are integers that can be represented in the type, for which the mathematical value 2*x cannot be represented. So something has to give. In C and C++, a signed integer overflow is undefined behaviour,...


1

Your answer is nearly right. So x is an unsigned negative value of k bits. The smalles possible value of x is representet by the 2's Complement number 1000....0. If you multiply this number by 2 (or logical shifting it by 1, which is equivalent) you end up with the number 0000...0 which is 0. And therefore (x*2)<0 is false.


0

It's helpful to think of the number in the following representation( I choose 3.75 as an example): $$2^0 * 11.11_2 = 3.15$$ Now $2^0 = 1$ so we can just write it in front of the number. Now we want to normalize $3.75$ meaning we need to transform it to $2^e * 1.m$ where $e$ and $m$ are some bitstrings. Notice that we can shift a binary number to the left by ...


1

Some answers suggest using the logarithm of n. Now guess what is the first thing your computer does if you try to calculate log n: It converts n from an integer to a floating point number, and from the exponent of the floating point number you get the number of bits immediately. So whatever time complexity there is, you have spent it already before even ...


0

N is 4 and b is 3. The formula holds perfectly fine. Nothing is wrong. The reason to "round up" in the context of the numbers you have is because of the inequality. 2 would not satisfy x ≥ ~2.32. But 3 would.


2

The number of bits is always an integer, so you have to solve for that equation over the integers. Thus, use $\lceil \lg (n+1) \rceil$.


Top 50 recent answers are included