64>>1 =32
I understand it.
But why,
27>>1 = 13
and not 13.5?
64>>1 =32
I understand it.
But why,
27>>1 = 13
and not 13.5?
Because its job isn't to divide by two. That is just a side-effect of its real function, which is to shift the bits of a number to the right by one. Not to mention that an integer can never be 13.5
Devoted my life to programming...
And, beware: Right shifts are 'undefined behavior' if done with signed integers. Most compilers translate right shifts with signed integers to "arithmetic shifts", meaning the sign bit (the highest bit) is copied. So -1 >> 1 will still result in -1. The value -1, using two's complement representation is 0xffffffff (for 32 bits integers).
But other compilers/processors can do a 'logical shift', resulting in +2łą-1 (0x7fffffff).
Anyway, both results tells us that right shifts aren't a division by powers of 2. And left shifts aren't multiplications by powers of 2 neither. Take the unsigned 0xffffffff (unsigned 4294967295), when shifted to the left 1 bit will result in 0xfffffffe (unsigned 4294967294).
Always do right shifts with 'unsigned' integers to avoid this. And never think of shifts as multiplications/divisions (unless you know what you are doing!).
You can only use the shift operator on integers. It doesn't make much sense to "shift" a floating point value. So where did that .5 go? It was discarded. If there's a 1 in the lower-most bit, it's shifted out and lost.
Read this wikipedia page on the subject.
Devoted my life to programming...
Study "two's complement representation", as GReaper suggested, and ask yourself: What happens when you subtract 1 from 0?
Last edited by flp1969; 04-14-2019 at 07:34 PM.
Yup! it is a way to indicate negative, zero and positive numbers. In one's complement, we used MSB for sign. But to carry that sign along was troubling. So, Two's complement was adopted as substitute. What happens? We take a number for example 2 and in binary 010 if for a moment consider 3 bits, now one's complement of 010 is 101 and add 1 in one's complement 111, is we get as two's complement and those great brain programmers decided to use it to indicate -2. Am I right?
Two's complement is resembling with the concept of floating point value in computer in my view. Floating point value is representing as scientific notation. So, 8 bits to represent to power. Now we use biased binary or offset binary to represent or IEEE754 ANSI i.e. 8 bits 2^8 = 256 or 2^7-1 = 127 shows offset for negative numbers. Now 128-256 binary patterns are for 0 and positive numbers. Now 129, i.e. in binary 1000 0001, might be showing +1 power. If I am not wrong.
Correct me if I am wrong, but I'm feeling like you are not getting it... A one's complement is not just setting the MSB, it's inverting all of the bitsYup! it is a way to indicate negative, zero and positive numbers. In one's complement, we used MSB for sign.
If we have a 4 bit number (just for this example) representing the number 3, we have...
Its one's complement will be (wiki)Code:3 => 0011
Its two's complement will be (wiki)Code:~number =1100
Code:~number + 1 1101
Last edited by Click_here; 04-15-2019 at 07:41 PM. Reason: grammar
Fact - Beethoven wrote his first symphony in C
Elementary decimal subtraction: How do you subtract 8 from 22? You cannot subtract 8 from 2, so you BORROW '1' from the next right digit:
In binary it works the same way, where "borrow" is a state kept by the processor. With single bits 0 - 1 = 1 + borrow=1. So, in a 4 bits word, 0000 - 0001 = 1111 + borrow=1), where this last bottow comes from the hypothetical bit 5.Code:22 -> (2-1) (12) -08 -> - 0 8 ----- ------------ 1 4
If you use only the highest bit as an indicator of sign, like -1 = 1001, the math will be wrong for 0000 - 0001. If you use one's complement, -1 = 1110, and still wrong. That's why two's complement is the right way to represent negative values.
The two's complement comes from the equation:
But, can be summarized in a single rule: Negative integers are represented by inverting all bits and adding 1.
Note this a representation. The actual value, in strict binary, is always positive (there is no way to insert the character '-' in a binary number, inside your computer). That said, signed int and unsigned int holds the same binary value. What is different is how they are interpreted...
Last edited by flp1969; 04-16-2019 at 05:58 AM.
Actually you would better use bit-wise operators for unsigned values.