JavaChallenge41
Try to solve the challenge by yourself first.
Looks pretty simple, but did you know it already?
The answer is —
Got Challenged!
Yes, it’s not Already knew
. Not only this, but the following are also true —
1. -0==0.0
2. -0==0
3. -0==0.0D
4. -0==0.0f
5. 0==0.0f
6. 0==0.0D
7. 0==0.0
8. 0.0D==0.0f
But how? How 0, 0.0, -0.0,-0, 0.0D, 0.0f are stored in memory?
If the above mentioned are true that means all are pointing to the same value which means 0=-0=0.0=-0.0=0.0D=0.0f.
Let’s find out the answer to how?
We know that decimal values are stored in binary format in memory, so let’s find out the binary values of 0 and -0 considering the 8bits machine.
0 = 0000 0000
To get the two's complement of a negative binary number, the bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value, ignoring the overflow which occurs when taking the two's complement of 0.-0 = 1111 1111 (Bits of 0 are inverted) + 1 = 0000 0000 (Ignored the overflow). That means 0=-0.
Reference — https://en.wikipedia.org/wiki/Two's_complement and Binary Number System
Let’s find out how to write a floating-point number in binary —
Let’s take an example 4.25 Where, 4 is an integral part and 0.25 is a fractional part.
To convert an integral part into binary, just follow the previously discussed method. Using that method, we can represent 4 as (100) Fractional part (0.25) - To convert the fractional part to binary, multiply fractional part with 2 and take the one bit which appears before the decimal point. Follow the same procedure with after the decimal point (.) part until it becomes 1.0.Like,0.25 * 2 =0.50 //take 0 and move 0.50 to next step0.50 * 2 =1.00 //take 1 and stop the process0.25 = (01) 2So, 4.25(Floating point number) = 100.01(Binary number)
Reference — http://cstl-csm.semo.edu/xzhang/Class%20Folder/CS280/Workbook_HTML/FLOATING_tut.htm