Podcast Lookup

How Java Stores Negative and Floating-Point Numbers | Java Memory Representation Explained

The Java Developers Hub

  • December 3, 2026
  • 58 min
  • Episode 4
Episode page
About this episodeHave you ever wondered how Java stores negative numbers and decimal values inside computer memory? Understanding how data is represented…
Have you ever wondered how Java stores negative numbers and decimal values inside computer memory? Understanding how data is represented at the binary level is an important step toward mastering Java programming.In this episode, we explore how Java represents negative integers using the Two's Complement system and how floating-point numbers are stored using the IEEE 754 standard.You'll learn how computers handle positive and negative values, how decimal numbers are represented in binary, and why some floating-point calculations produce unexpected results.Whether you're a beginner learning Java or an aspiring software engineer interested in computer fundamentals, this tutorial will help you understand what happens behind the scenes when Java processes numeric data.What You'll Learn
  • How computers represent numbers in binary.

  • How Java stores positive and negative integers.

  • Understanding signed and unsigned binary representation.

  • What Two's Complement is and how it works.

  • How negative numbers are calculated using Two's Complement.

  • Understanding integer data types in Java.

  • How Java stores floating-point numbers.

  • Understanding the IEEE 754 floating-point standard.

  • The roles of sign, exponent, and mantissa (significand).

  • Differences between float and double.

  • Why floating-point calculations can produce precision errors.

  • Practical examples of numeric representation in Java.

Who Should Watch?
  • Beginners learning Java programming.

  • Computer science and software engineering students.

  • Aspiring software developers.

  • Developers interested in computer architecture and memory representation.

  • Students preparing for programming and technical interviews.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

How Java Stores Negative and Floating-Point Numbers | Java Memory Representation Explained

00:00—
  1. Speaker 1

    Welcome back Coder Army to our Java series and today we are going to talk about a very interesting topic and that is how we represent negative numbers and floating point numbers in binary and how they are stored in memory because we talked about positive numbers and it was very straight forward We will take a positive number and take out its binary. And it will be saved in the memory as well. But what about negative numbers? Or what about floating point numbers? And how do we express them in the form of zeros and ones? Because we know that the computer only understands zeros and ones. It doesn't understand any decimal or any negative or positive. So how do we represent these things on the basis of zeros and ones? This is very interesting. Secondly, this is something that is asked a lot in interviews, especially from the point of view of Java, because they want to know that you internally understand how things are stored inside. Okay, so it will be a lot of fun. So let's quickly come to the screen. Yes, so look, first of all, obviously, I have made a demo.java file, it is a completely empty file. Okay, and you know, whatever code we write in it, we have to focus on it, we have to ignore it for now. So, like the last class, first of all, I'll make a float number, let's say f. And I'll put its value, let's say 8.125. And you know, we have to put an f to denote that it's a floating point number. Otherwise, the processor will double treat it. And finally, we can print it. And to print it, you know what we write? System.out.println And we write f directly here. Now, you know, when we print it, it will be 8.125 on our screen. which is expected, right? So, let's do it. Yes, so our 8.125 is here. No problem. If we write 0.7 here, right? Now, we had talked once in the last video that the floating point numbers are single precision numbers. So, if your number is very small or very big, then its precision gets spoiled, right? So, let's see once if we try to print on 0.7f. So, let's see once if 0.7 will come or not. It is showing 0.7. We think everything is working fine. The way we assign a literal in our variable, it will be saved in the memory. And when we try to print it, it will be converted into a decimal. But there is an issue that Java is hiding from us. He is trying to hide something from us and we have to find out about it. So first of all, we do some changes in this. First of all, I write printf instead of println. Those who come from C background know what printf is. And now I will print this f, not directly, but I will try to give it a formatting. Let's say, I am saying, let me explain to you what it means. Okay. This means that I want to print this f but not directly. I want to see its value till 20 decimal places. places to express, then I should have 0.7, then 00000 up to 20 decimal places, right? Similarly, if I have the value of f here, what did I do last time? I took 8.125, right? And if I had expressed it in 20 decimal places, then what should it have been? 8.125, then 00000, right? For remaining places. So let's do this again. First, I'll show you again for 8.125. This is the only meaning of this. Understand the formatting. F means I am going to print a floating point number. 20 means I want its value up to 20 decimal places. Now let's see this. Sorry, some extra print is done. Now let's run this once. See this. Absolutely correct. What did we expect? 8.125 and after that all other zeros. Now, if I do the same thing for 0.7, which we were doing earlier, then should we get 0.7, 0, 0, 0, 0, right? Are we getting this? Look, not at all. We are getting 0.6, 9, 9, 9, 9, something random values and up to here. So, we are getting such a big value, why are we getting this? Why are we not getting 0.7? And if you look carefully, this value is very close to 0.7, but not 0.7, right? So, what is happening in memory? How our 0.7 is being stored? When we want to retrieve it, we are getting this kind of value. And remember when we were putting print talent here and not writing formatting, then Java was hiding this value from us. Java was saying that I will round off this and give it to them. Right? So we were getting 0.7 by rounding off. But in memory, it is being stored exactly like this. Okay? So now let's see how negative numbers and inverse floating point numbers are stored in memory. Negative numbers are simple, if you take minus 84 or 42, it will be printed as it is. So let's see both things quickly. First of all, let's talk about how negative numbers are stored in memory. So first let's talk about the negative number. So for that, I take a negative number. Let's say we store numbers in integer. There are four things in integer, byte, short, int and long. And the smallest is byte, so we take it in byte. So byte b is equal to minus 42. Okay. I wrote it. Now I want to see how this minus 42 is represented in memory. So let's understand this carefully. Okay. Now, first of all, we know that the range of bytes is 8 bits. So, 1, 2, 3, 4, 5, 6, 7, 8. In this 8 bits, our bytes will be stored. And we know that if we talk about its proper range, then from minus 128 to plus 127 numbers, you can express in bytes. So, minus 42 is lying in this range. So, I want to see how we can express minus 42 in the format of zeros and ones. Okay, so the first step, let's see how we represent plus 42. Because to represent minus 42, first we have to understand plus 42. Okay? So first of all, I'll make 8 places. 3, 4, 5, 6, 7, 8. And we had already done all this that here 1's place is 2's. 4, 8, 16, 32, 64, and 128. These 2's keep increasing with power. And we have to represent 42. So for 42, 1 is 32. Plus 8 is 40. And this plus 2 is 42. And the rest will be zeros. We can do it this way. There is another way to get the binary from decimal. You can do it this way, if the number is simple. But if the number is a little complicated, then it will be difficult to get it. So for that, we can do this way. You don't have to do anything. We have to keep dividing 42 by 2. Because we are taking it in the base of 2. And whatever remainder will come, we will write it on the right side. This is completely divided by 21 and the remainder is 0. And then we have to divide 21. So 21 is completely divided by 2 times 10 is 20 and the remainder is 1. Now this will be completely divided by 5 times 10 and the remainder is 0. Then 2 times 2 is 4 and the remainder is 1. And then 2 times 1 is 2 and the remainder is 0. As soon as 1 is there, our value is over. Now you go in this direction. You travel in this direction and you will get 1, 0, 1, 0, 1, 0. Okay? This is the representation of 42 So if you look carefully, we also got something like this 1 0 1 0 1 0 Right? Obviously, we represented it in 8 bits So here also we put two zeros to represent in 8 bits Because no matter how many zeros you put on the left side, it doesn't make any difference So you can also get it out like this if you want a simple way To convert any decimal to binary So we got the final number 00101010 Cool Now, this means that if we write this thing, as we have written this statement, byte B equal to minus 42. Not minus 42 for now, we were talking about plus 42. So, let's say if we have written in memory, byte B equal to 42. Something like this will be written in our code. First of all, this 42 will be converted to binary which is 00101010. After that, it will be stored in a container in a memory. And the name of that memory is B. So here 00101010 is saved. And when we try to print it, when we write this command, let's say system.out.println and we say that we want to print b, then what will happen? As soon as we say that we want to print b, then the compiler of java will go and see what is stored in the memory of b. So it will see that this is the store. And it will try to convert it back to decimal. And as soon as it converts to decimal, it will get 42. And what will we get on the screen? We will get 42. Absolutely good. But how will we store the negative number? So now we can talk about negative minus 42. If you understood this much. Our next question is, byte b equal to minus 42. We have to store this. So to store this, the way we use in Java, we call it two's complement. So, we take out the two's complement of any number, any negative number, and then store it in memory. Now, what is this two's complement, brother? Nothing happens, it is very simple. First of all, what we will do is, we will ignore this minus 42 sign. We will consider it as simple 42. And we will take out the binary representation of 42. So, what was the representation of 42? 00101010, okay? So, I will write 00101010. of all, we will remove its first complement. First of all, we will take out its first complement. Now, first complement is nothing. You just have to convert all zeros to 1 and 1 to zeros. Means, we have to reverse all the bits. We have to invert them. So, let's quickly do this. This becomes 0, this becomes 1, this becomes 0, this becomes 1, this becomes 0, this becomes 1, this becomes 0, and this becomes 1. What did you get in the final? You got this. This is your first compliment. I will write it properly. So we have taken out the first compliment. Now we have to take out the second compliment in which we will actually store it. Now how do we take out the second compliment? To take out the second compliment, we add 1 to the first compliment. This means that the compliment we took out is 1 1 0. 1, 0, 1, 0, 1. This is what came out here, okay? In this, I just have to add 1. If you don't know how to add binary numbers, it's very simple. Like if you add something in the normal decimal number system, then if you do 9 plus 1, then we know that there can be 0 to 9 numbers in decimal, right? If you want to do 9 plus 1, then basically what do you want to do? You want to go to a new number which is in our binary. Sorry. You want to go to a new number which doesn't exist in our decimal system Because 10 is made up of 1 and 0 So in that case we used to take carry So 9 plus 1 we used to call 0 And we used to say carry is 1 And we used to write carry here So something like this 10 used to come Same thing we will do in binary Now because in binary we can have only two numbers 0 and 1 So any number which is greater than 1, like if I add 1 to 1, then we don't have 2. So I will say 1 plus 1 is 0 and I have taken carry of 1. Simple. So this is the way we are going to apply here. What will we do? 1 plus 1, we said 0, carry is 1. Now 1 plus 0, obviously 1. All the other numbers will be the same because there is nothing left in it. So our 2's complement is something like this. 1, 1, 0, 1, 0, 1, 1, 0. Okay? Now our computer in any container where it wants to store it will store this number. How? So now what will happen? As we talked about that, as soon as we write byte b equal to minus 42, then first of all, internally, it will take out the two complements of this minus 42. So, it will come 11010110 and then it will store it in some memory. 11010110 and will call it b. Something like this. Okay, this is understood. But brother, now tell me one thing. When the computer stores this number like this, and if we write a statement like this, and again we write a statement like this, that we say system dot out dot, Print and we said we have to print b Now as soon as I wrote system.out.println Our java compiler will go to this container and see what is stored here And somehow it will try to remove minus 42 Now before moving ahead, let me tell you one thing How the computer will know that this number is a negative number and not a positive number So when it has to retrieve it So how to do it, let me explain it to you First of all, let me tell you how the computer will read the number 11010110. The computer will see what its MSB is. Most significant bit is your leftmost bit. And least significant bit is your rightmost bit. What is the meaning of MSB and LSB? Let me explain it to you. Think like this. When you talk about decimal number system. If I take a big number like 2320. Or 2321. Same concept if I apply here, then MS becomes leftmost bit and LS becomes rightmost bit. Least significant bit, most significant bit. Now what is the meaning of most significant and least significant? It doesn't have any meaning. It means that We have this number here. Its significance is the least. It has more significance than that. Then it has more significance than that. What does significance mean? If I replace this number 2, 3, 2, 1 with 2 here, i.e. plus 1, the LSB, So our number only increased from 1. So it didn't make much difference. But if I increase my least significant from 1 in 2321, then our number will be 3321. So 3321. Properly increased from 1000. So if we increase 1 from here, then the number increased from 1000. But if we increase 1 from here, then the number only increased from 1. So its significance is very high. That's why it's called MSB. And here significance decreases, so we call it LSB. So, we use the same concept in binary. Here also, the leftmost number will have more significance and the rightmost will have less significance. So, Java tells us that the most significant bit, if it is 1, then it means that our number is a negative number. And if it is 0, then it means that our number is a positive number. Simple. If I write it properly, then If MSB is 1, then it means negative number. And if MSB is 0, then it means positive number. This way, the computer finds out that the number stored in the memory is a positive number and this is a negative number. Very good. But now you will ask, if we apply the first thought principle again, It is also possible that this first bit 1 is not 1 because it is a negative number, but because of a positive number, if we are taking it out binary, then first bit 1 can come. Absolutely right, a valid question came to your mind. But I am telling you, this can never happen. Why? I will tell you again. See, what was the range of a byte? 8 bit means 1, 2, 3, 4, 5, 6, 7, 8. 8 bit can store 8 bytes, so this is your 8 bit. I am saying that The biggest number in this, what can you represent? Remember the range, from minus 128 to plus 127. So the biggest positive number, which I can express in this, is plus 127. And to do plus 127, if I remove this once, and I write all the range here, that this is our 1, This 2, this 4, this 8, this 16, this 32, this 64 and this 128. Now, I have to represent my biggest number which is 127. For this, what I have to do? Everyone has to do 1 except 128. Something like this. So, now see. All our bits can be 1, except 128th bit, which means the most significant bit will always be 0. Okay, we can never make it 1, because if we make it 1, and make the rest 0 as well, then it means we have represented 128. Suppose I make all of this 0. Okay. And I make this one 1. So this number means that I have represented 128. Because this is just 1 and the rest is 0. But 128 is out of this range. That means, I have to make this number I can't do 1 in terms of positives. Right? So, in all my positives, my most significant bit will always be 0. Now look, if I want to represent any negative number, let's say negative number from minus 128 to minus 1, I can represent anything, right? If I want to represent any negative number, then what I used to do, first of all, it would ignore its sign and remove its binary. So, let's say if I want to represent minus 127, then I will remove the binary of 127 first, which is this, which we have removed, right? That means our most significant bit is 0 only. Then I would remove its first complement. In this case, I would make all 0s as 1s and all 1s as 0s. Right? In that case, see what we did. We changed its most significant bit to 1. Means its sign is reversed. Now if I see that MSB is 1. It means number will be negative only. Now you will say that I will add 1 also. Of course you will add. But by adding 1, there will be no difference in its sign. There will be difference in one case. Which is 0's case. But we will discuss it later. But in other case, you see. This number will always be 1 for negative numbers And 0 will be for positive numbers Right? So now you must have understood the whole story So now see, how did I represent minus 42? First I took out the binary of 42 Then I took out its first complement I did 1 for all 0s and 0s for all 1s And then I took out its second complement by adding 1 in the first complement And we got this number So we saved this number in the memory Simple Now you were saying, if I do system out and println, what will happen? So nothing will happen. Let's see this too. Let's assume that the B in my memory is saved like this. What was our final number? 11010110. So let's write it. Let's write it here. 11010110. Now as soon as I write that I want to print this B, what will be my command? System.out.println. So, first of all, let's see what the computer will do. The compiler of Java will see what is there in this memory. and what is there in B. So, first of all, it will see whether its most significant bit is 1. It will say, yes, it is 1. It means that our number is a negative number. Now, when I have to convert the negative number from binary to decimal, then I will have to do this two-step process again. First of all, what will I do? First of all, the compiler will do this work from inside. First of all, it will remove the first complement of the upper number. So, all the ones will become zeros. So, what will happen? Zero, zero. 1, 0, 1, 0, 0, 1. Something like this. I have reversed all of them. Then it will take out its second complement. It will add 1 to this. So 1 and 1 is our 0, carry 1. And this will come here, 1 plus 0, 1. And the rest of the numbers will be the same. So finally this number will come out. And if I convert this number into decimal, then what will come? This is my 1st place, 2nd place, 4th, 8th, 16th, 32th, 64th, and 128th So what is my 1? My 32 is 1, 8 is 1, and 2 is 1 So 32 plus 8 plus 2 is 42 So, the number is 42. Now, it had seen earlier that the first one was 1. So, it put a negative sign here. So, our minus 42 is retrieved. So, this is the whole story. When you go to print minus 42 or any negative number, what does it do? First, it takes out its first compliment. Then, it takes out its second compliment. It puts a negative sign and your work is done. And that's how we store negative numbers into the memory. Right? Now, one question must have come in your mind and it should definitely come. Why are we taking out the second complement? We could have taken out the first complement and stored it in memory. Right? Absolutely. This specific thing, we took out the second complement to handle one particular condition. And what is that condition? That condition is 0. Let's see. So look, in byte, again 8 bits are there. 4, 5, 6, 7, 8. Here, I have made 8 spaces to represent a byte. Now, suppose if I write 0 in my code. Let's say I write byte b equal to 0. Now, what is the simplest way to represent 0? That I make all bits 0. Simple, right? This will be it. I have represented 0. All bits. 1 bit, 2nd bit, 4th, 8th. 16, 32, 64, and 128. I have made all the bits 0. It's absolutely correct. Now, suppose if someone had written that byte b equal to minus 0. Obviously, we all know that minus 0 is nothing. It's invalid. It's according to maths, right? But if someone had written it, then what will he do with minus 0? He will see that compiler is minus, which means it's a negative number. And what I have to do is represent it in once complement. So, what will he do in once complement? He will make all the zeros 1. So, everything will become 1. 5, 6, 7, 8. See, 1, 2, 3, 4, 5, 6, 7, 8. Yes. So, we did everything as 1. And we stored it in memory like this. Simple. But as soon as we stored it in memory, it means we stored a negative zero. This is wrong, right? Negative zero is nothing. So, if someone is writing negative zero, then we should consider it as zero only. So, there should be only one case of zero. So, in that case, we took it to the second complement. Now, see, we took out the first complement first. So, this is our first complement. Now, to take out its second complement, we just had to add one. Now, as soon as you add one, see what will happen. One plus one, zero, carry one. Again, 1 plus 1, 0 carry 1. 1 plus 1, 0 carry 1. And see, it will keep on going. 0 carry 1. 0 carry 1. 0 carry 1. 0 carry 1. And here also 0. And again carry 1 will come. And here will come simple 1 extra. But now you think about it carefully. How many bits are there? 1, 2, 3, 4, 5, 6, 7, 8, 9 bits are there. But our byte can store only 8 bits. So what will we store in bytes? Only the first 8 bits. First leaves significant bits and we will ignore this. So as soon as we store this in memory, 00000000, we will store this in memory. This means what we actually stored in memory, we only stored 0. Now if our compiler, if we want to print this number, if we want to print this number, then what will happen? The most significant bit is 0, which means it's a positive number. And when it converts it from binary to decimal, it becomes 0. So negative 0 is still 0 in our case. Because we will forget this 1. This 1 is not there. Because 8 bits have disappeared till here. So because this case can come, so that we can invalidate our negative 0, and there is only one condition of 0, Even if we write a negative zero, we can express it in the form of zero. That's why we expressed the numbers in the form of two's complement. We expressed the negative numbers. Right? Now, we talked about the special case. In which we said that as soon as we find the first complement of a negative number, our most significant bit becomes one. Like we took the example of minus zero. We found the first complement of minus zero, so everything became one. That means the most significant bit also became one. Right? Now as soon as the most significant bit became 1, we took out its 2's complement and in this case the most significant bit became 0 again. So this is the same case and when can it happen? When you add plus 1 in it and all the numbers become 0 here. See 0, 0, 0, 0, 0, 0, 0, 0. Because in all of them carry, carry, carry, carry kept on running. Right? And in the last bit also, carry was carried so here also it became 0. But that case can only come for 0 and an extra number will come here outside. In that case, we ignore the 9th bit and store only the 8th bit in memory. And that number is 0, not negative 0. Negative 0 is nothing. We invalidate it. So, it means that 2's complement is a very good way to represent negative numbers. In this case, we handle every condition, negative, positive and zeros. And no special case comes in this. If the most significant bit is 1, that means your number is negative. And if it is 0, that means your number is positive. We get to know this. Okay. So, I hope you understood this. Now, let's move to our more interesting question. How do we represent our floating point number? Okay. Floating number. Now, we had seen two cases in this. Which we had run and seen. Once we write, let's say, float. Let me write it in white. Let's say float f is equal to 8.125. And let's say float f is equal to 0.7. I forgot to write f here. So if I write two numbers like this, then what happens behind the scenes is how our floating numbers are stored in memory. Why am I taking the example of float? The same thing can be done with double. Double is 64 bit and float is 32 bit. So it will be easy to understand. So first of all, we will see this. Because this was our as it is store. When we stored it and retrieved it, we got 8.125. But in this case, when we stored it and retrieved it, we got 0.6999 something something instead of 0.7. We didn't get exactly 7. So we will see both cases, first safe case and second edge case. So first of all we will see how our floating point number is stored in memory. So first of all our floating point number, we know that it takes 32 bits. So let's say I make a space of 32 bits. You can imagine this is 32 bits. From here to here. Cool. Now, let's divide this 32 bits into how all our things will be stored. So, first of all, 1 bit from this 32 bit is used as a sign bit. I will tell you how. After that, the next 8 bits are used to store our exponent. And the remaining 23 bits This is called Mentisa. To store our Mentisa. Now, don't worry about the name of this. What is a mantissa? What is an exponent? We will cover it all nicely. Okay. I just told you a flow first. That any decimal number is stored in 32 bits. If it is a floating point number. And how is it stored? First bit we will keep as a sign bit. As we read in negative number. If it is 0, it is a positive number. And if it is 1, it is a negative number. Next 8 bit we will keep as an exponent. And rest 23 bit we will keep as a mantissa. Okay. So, let's see how the whole conversion will happen. First of all. Our number was, let's write our number first here. F equal to 8.125. Okay. We have to store this in memory. So, we will go step by step. First of all, what we have to do? Look. First of all, we have to convert this 8 and this 0.125 into binary. Okay. Now, the trick here is that whatever is on the left side of the decimal, we have to convert that. If we write 8's binary, then what will happen? 1, 0, 0, 0. Right? 1, 2, 4, 8. Point. I have written 8 in binary as 1, 0, 0, 0. How many times have I taken this out? Now, how can we take out the binary of 1, 25? I will tell you the method of this too. Whatever number decimal's right side. In that, if we take out the 8th baron, if we take out the 8th baron, then we divide, 2, 4, 8, here 0 comes, 2, 2, 4, here 0 comes, 2, 1, 2, here 0 comes, then 1, 0, 0, 0. We take it out like this and it comes out. So, to take out its baron, we do the opposite. We multiply instead of divide. So, for example, 0.125, now I will multiply 2 here, not divide. So, 0.125, into 2. So what will happen? It'll be 0.25. And whatever number is on this side of the decimal, I'll write it separately. Here's our 0. Now if we multiply 0.25 by 2, then it becomes 0.5. Here I have written 0 because the number here is 0. Now if I multiply 0.5 by 2, then it becomes 1.0. So here I will write 1. I have written 1 here. Now see, as soon as the number 1 is here, I have written 1 here. The next time I go here, I will make it 0 again. So 0.0 does not happen at all. So here my flow is over. So my binary turned out to be 0, 0, 1. Okay. You remember what we had to do. Whatever number we take, we do it into 2. Whatever comes, whatever number was on the left side of the decimal, we write it out. Then we do 0.25 into 2. Whatever we left side comes out. Let's do 0.5 into 2. Let's get rid of the left side. Now, as soon as we write 1 out, if we take 1.0 instead of 1.2, then we don't stop. We start again. But this time, let's do 0.2 into 2. Okay? We'll see that later. If you've already practiced this, then it's a good thing. Otherwise, practice a little. How do we convert the right side number from decimal to binary and the number before decimal, i.e. the left side number, to binary. So, its binary will be 0, 0, 1. Okay, the way we read this is also the opposite. We read it top to down. Like we used to do bottom to up, we do it top to down. So our binary is 001. We wrote 001. Okay? This will come with practice, no problem. If you don't know. But let's assume we took out 8.125's binary. 1000.001. Very good. First step completed. What was the first step? I keep writing this along with it. Find Binary of above number. So we have done this in step 1. Step 2. Now see the second step is very main. Make it in the form of. Now see we have to bring it in below form. Below form is 1.xx into 2 to the power exponent. Something in this form. 1.xx into 2 to the power exponent. In this form, we have to bring this number, which we have converted into binary. Okay, so how can we bring it? It is very simple. What do we do? Look, here our 1.xx To get it in the form of 1.xx, let's take this decimal place 3 places back. As soon as we take it 3 places back, our number will be 1.000 and then 001. Now as soon as we take it 3 places back, then what will be the maths accordingly? x2 to the power 3. Why didn't it become 10 to the power 3? Because we are not talking about the decimal number system, we are talking about the binary number system. And in binary, things always increase with the power of 2. If we are talking about normal, suppose if it was 33.5, then I would have written 3.35 into 10 to the power 1. But in this case, I will do 2 to the power. So, what did I write? 1.00001 into 2 to the power 3. Very good. So, I have expressed it in this form too. Now, let's see what the third point says. Now, the third point says that whatever your exponent has come, you have to add one thing to it. Add, which we call bias. Bias to the exponent. Now, what is bias and how do we remove it? We will discuss that in a little while. But for now, you assume for floating, for floating point number, float, Bias is equal to 127. How did this come? I will tell you in a while. Now you keep following this rule. So the number we got here, we have to add bias to it. What was our exponent? 3. So if we do 3 plus 127, then we have 130. And we will convert this 130 into binary again. So what was the method of converting into binary? This method was simple. So here will be your 65, here will be 0. Right? and here will be your 2,3,6,2,2,4,1 will be here here will be its 16, here 0 here will be 2,8,16,0 2,4,8,0 2,2,4 your 0 and 2,1,2 ok here your 0 so your this binary will come 1,0,0,0 3 times 0 and then 0,0,1,0 0,0,1,0 ok so our this binary came this is our exponent Let's write it separately. So we have taken out our exponent, we have expressed it in this form, we have expressed it in this form. Now our last step comes, fourth step. That we have to put values in it. Place value in memory. Now let's go back to our memory and see what we had. We had a 32-bit memory in which the first bit was a sin bit, then it was an 8-bit, so this is 1-bit and this is 8-bit. This is called an exponent, this is called a sin, and this is called a mantissa. And our mentissa was 23 bit. So look, we have this number, this number, 1.00001 into 2 to the power 3. And we added 3 to 127 by 22, so it became 130. And 130 exponent became this. If we want to express this, then first we have to see what is sine bit. Now this number was positive. Obviously, look at this, 8.125. So its sine bit became 0. So first of all, 0 will be stored here. Now here we will store the exponent in 8 bits. And our exponent is 1 0 0 0 0 0 1 0. How many bits are these? 1 2 3 4 5 6 7 8 8 bits. So here I will store it. 1 0 0 0 0 0 1 0. This is it. We have saved the exponent. Now, we come to MENTESA. What is MENTESA? MENTESA is nothing. We had expressed it in this form, 1.xx. This part of xx, we call it MENTESA. We don't need to represent 1 here. Because we know that our syntax, our form is 1.xx. So, whatever will happen before this, it will be 1 point only. So, we just represent this. 0, 0, 0, 0, 0, 1. So, we write here. 0, 0, 0, 0, 0, 1. So, how many bits are there? 1, 2, 3, 4, 5, 6 bits are there. But we have to represent in 23 bits. So, we will make all the rest 0 here. Okay? Something like this. Because this is the right side part of the decimal, so we can put any number of 0 on this side. It doesn't matter. Right? So, this way we have represented all the numbers in memory. Okay? Now let's see, when we write system.out.println Now I have to print my f back. So how will this number be retrieved? Just like the java compiler goes to the memory, reads it and how will it convert it back to 8.25? Let's see, it's very simple. Now, as soon as we write this, the compiler will go to the memory and read this. And see how it will convert this. For this, the simplest formula is minus one power sine into one plus mentissa into two to the power exponent minus bias. This is the formula. Okay? This is not a special formula. It came out in the same way as we decoded it. Look, if we add the power of minus 1 to the sine. And if the sine was positive, then we will add 0. And anything raised to the power 0 is 1. Right? So, this number is 1 into. Now, whatever will be multiplied by 1, that number will remain the same. Right? So, this number doesn't make any difference. But if we add the power of minus 1... Let's add 1. If the sign was negative, then minus 1 to the power of 1 is minus 1. And anything multiplied by minus 1 becomes negative. So, we will make it negative in this way. That's why we used this term. Now, why 1 plus mantissa? Because we had stored mantissa here without one point. We had one point xxx format. We had only stored xxx here. We will add this 1 here. And why 2 to the power of exponent minus bias? Because when we took out 2 to the power of 3, we added bias to it. So, if we want the exact exponent again, then we will minus bias. So, you must have understood why we used this formula. So, let's put our values here. So, look. What does minus 1 mean for us? Minus 1 to the power of 0. So, we will put minus 1 to the power of 0 into 1 plus... 1 plus, now see, we have to get the mantissa. We will convert this mantissa back to decimal. So, we will do that in a while. So, now I will leave it empty. So, here mantissa will come. Into 2 to the power. Now, what was our exponent? Our exponent was this. So, what is this? We will write this once. 1, 0, 0, 0. 3 times 0, 4 times 5, 1 times 0, 6 times 7. If you subtract the decimal from the binary, then you get 130. Because this is 128 bit and this is 2 bit, so 120 plus 2 is 130. And if you subtract the bias from 130, which is 127, then you get 3. So 2 to the power 3. So this is also subtracted. Now we will put the part of mentissa as well. Now look, this is the number of MENTESA which comes in this part of 1.XXX Now if I want to convert this back to decimal, then the way to do it is When is 1 coming to the right side? So this is 2 to the power of minus 1 This is 2 to the power of minus 2 Okay? If you see, if I am going from 0 to this side, right? If we take out its binary, then how would we do it? We would do it like this. This is the first place, which means 2 to the power of 0. This is the second place, which is 2 to the power of 1. This is 2 to the power of 2. This is 2 to the power of 3. We would take it out like this. But if 0.324 was written like this, then we would take out this side like this. 2 to the power of minus 1. 2 to the power of minus 2. 2 to the power of minus 3. Did you understand? This is just a basic conversion. And this is the same in every language. This is because basic is your discrete mathematics. It is binary mathematics. So, in this case, what will we do? We will see how far we can go with the power of 2. So this is minus 2. In the same way, let's change the color. This is minus 3, this is minus 4, this is minus 5, and this is minus 6. And the rest is 0. So our 2 to the power of minus 6 is 1. So what is its value? Let's write it here. 2 to the power of minus 6. Which we can write as 1 plus 1 by 2 to the power of 6. Into 8. And... Now, what is 2q over 6? 64 is 1 plus 1 by 64 into 8. If you do 1 by 64, then your 1 by 64 will be 0.015625. I have calculated this. And then if you add 1, then it will be 1.015625. And when you multiply 8 in this, now when you multiply 8 in this, your answer will be 8.125. which we had to remove, you can do it. Right? So see what we did. We started from here that the number we have is 8.125. Okay? We first followed this process and stored this number in memory. And when we tried to print it, it would have been retrieved by following this format and our number would have come back to 8.125. It might be a bit complex. It has more mathematics, especially binary mathematics. If you get used to it, then it is very easy to find out. And you get a very good question from the interviewer. How do exponents store in Java? Or how do floating point numbers store? We also understood the negative one. Floating point is a bit more complicated. But it gives a very good education. So we understood this. That as soon as we stored 8.125, we got that back. And we saw this in the code too in the starting. When we printed 8.125 in 20 decimal places, then what number did we get? 8.125 and the rest were zeros. But the problem was with whom? The problem was with our second number, which was, let's say I wrote float f equal to 0.7f. Why was this a problem? Now if your question is why was this a problem, then if you follow the same procedure, you will understand. Let's do it, it will be fun. First of all, we know that we have to go step by step. In step 1, we have to express 0.7 in binary. So express to binary. So in that case, what were we doing? Here we used to write, let's say 0.7. 0 will be the binary of 0 and what will be the binary of 0.7? Let's follow the same. If we multiply 0.7 by 2, then it will be 1.4. We have kept 1 of 1.4 here. So we have 0.4 left. If we multiply 0.4 by 2, then it will be 0.8. We have kept 0 here. If we multiply 0.8 by 2 again, then it will be 1.6. So we have kept 1. Now if we do 0.6 0.6 x 2, then again it becomes 1.2. We have put 1. Now what has come again? If you do it again, it will be 1.2. If you keep 1, it will be 0.2 again. And it will start repeating again. So basically what is happening is that your decimal number will be like this. Your decimal number will be like this. 1011 again 0011 means 01100. This will keep repeating 01100. It will keep on repeating infinitely. So there is no proper way to represent it. And now you must have understood that because of this problem, there will be a problem in the future. Let's see how it will come. So I told it that its binary will not come out exactly, it will come out in this way. So I did step 1. 1.xx into 2 to the power exponent format. So, let's do that. See, what is the number here? 0.1. If we just send this decimal here, then what will be our number? Our number will be 1.0110. 0110. And this will continue to run. Multiply by 2 to the power minus 1. Because what have we done to this? We have moved one forward. So, 2 to the power minus 1 is done. So, here we have done step 2 also. Now, what was step 3? Add bias. We have to add bias. Let's do that too. Now, what was the bias? It used to be 127, you know that. So, if we do plus 127 in minus 1, because we set bias in exponent, then it will become 126. Now, if we express it in binary, then what will be the binary of 126? 2, 6, 0, 12, 3, 0, 6. Here, your 0 will come. And 2, 3, 0, 6. 2, 1, 0, 2, 1. 2, 15, 0, 30. 1. 2, 7, 0, 14. Your 1 is done. 2, 3, 0, 6, 1, 2, 1, 0, 2, 1, and your binary will be like this. Which is 1, 1, 1, 1, 1, 1, and 0. Okay? Now how many bits are there? 1, 2, 3, 4, 5, 6, 7, and here we have 0 for 8 bits. Okay? You must have understood how we took out its binary. So your binary is also here. Now what was our final step? Express it in memory. Now we can express it in memory. So let's see again. Our memory looked something like this. We made this memory. First your sin bit, then 8 bit exponent and the rest is your mentissa. So look. What was our number? 0.7f. That means the number is positive. So here sin bit will be 0. Then we have to express the exponent in 8 bit. What is our exponent? This came. So let's write it. 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6. 5, 6, 0. Right? So, we have expressed the exponent as well. And then here, there was 23 bits for mentissa. 23 bits. Right? So, our mentissa came here. This part. Right? So, let's write it. What is our mentissa? 0, 1, 1, 0. 0, 1, 1, 0. Now, see. 4, 8. Again, 0, 1, 1, 0. 12. 0, 1, 1, 0. 16. Now, we have represented 16 bits. So, let's move this a little further. Okay? Again, what will come? 0, 1, 1, 0. This became 20. And we had 23 bits. So, 21, 22, 23. Look, our bits have finished till here. This means we can't represent any other number. So, our mantissa became this. Okay. I hope you understood. Because we are storing truncated data in memory. So, when we want to retrieve our compilers again, that's why we won't get exact value. Now, you can try it. If user writes on our system system dot out dot println and it writes now give me the value of f so our compiler will again go here and will try to retrieve it and the formula of retrieval we had taken out the formula of retrieval was minus 1 power sine into 1 plus mentissa into 2 to the power exponent minus bias this was it so if we fill the value in it then our value will come minus 1 power sine is 0 into 1 plus this we will take out later into 2 to the power This is our exponent. If you subtract this, what will it be? It will be 126. 126 minus what was the bias? It was 127. So, this will be minus 1. And the previous one, so minus 1 to the power 0 is 1. So, let's forget this. So, 1 plus whatever mentissa will be here, into 2 to the power minus 1. Something like this. We can write minus 1 as 1 by 2. Simple. Right? Now, let's try to subtract mentissa, which will be very big. See, this is your mentissa. If we take it out carefully, then it will look like this. First of all, this is 2 to the power of minus 1. Let's leave this. This is minus 2, minus 3. This is minus 4, minus 5. Then minus 6, minus 7. I am expressing where there is 1. This is minus 8, minus 9. Minus 10, minus 11. 12, 13. Let's leave this. 19, 20, 21, minus 22 and minus 23. So something like this. It will become a very big number. So if I express it in this number, then 1 plus 1 by 2 to the power. What was the first one? 2 to the power 2. I wrote 2 to the power minus 2 by reciprocating. By 1 by 2 to the power 3. And after 2, 3. After that, what was it? 6, 7, 10, 11. So let's write this too. So it will come here. Sorry. plus 1 by 2 to the power 19 and that's it. Right? No, there are 22 and 23 as well. So, 1 by 2 to the power 22 plus 1 by 2 to the power 23. This will come out. Into 1 by 2. So, whatever is complete, we have to divide it by 2. Okay? If you try to calculate such a big number, then the number that will come out is... So this will be the result of this calculation. In the end, it is 1 by 2, so I divided by 2 and finally we got this output. So you must have understood that this output is not 0.7, it is very close to 0.7. It is as close as 0.7 can be, but it is not exactly 0.7. And this is the issue we saw when we gave 0.7f a value of 0.7 in the form of 0.7f. And we got a number like this. It will be exactly the same till here, right? Because it will truncate till here. But you see, how floating point numbers are stored in memory. How interesting it was, right? And when you will be asked this in interviews and you explain this, then it will make you very different from those kids who just understand Java from the top. They never know how negative numbers or exponent numbers are expressed from inside. Okay? If you understood this, So, let me give you some questions. First, what was this bias? And how did we take the value of bias 127? So, let me tell you. Look, I told you that any floating point number is represented in 32-bit. So, this is your 1-bit, sign-bit. Then, your 8-bit is your exponent, your sign. And this is your mantissa. And this is your 23-bit. Something like this. Right? Now look, this 32-bit format of storing any floating point number, this is according to IEEE standard. Okay, according to IEEE standard. Now, this IEEE standard says that if I want to store any negative number, like if your exponent is negative, like in the case of 0.7f, our exponent 2 power minus 1 was coming, right? If you remember, then it says that I don't want to store any negative number, I want to make it positive. So, I will add a bias of plus 1 to 7 in this. So that even if your number is negative till minus 1 to 7, I can add 0 to it by adding plus 1 to 7. I just don't want to store negative numbers. That's why I add a bias in it. If the number is already positive, it will become a little bigger. So this is the rule of IEEE standard that I will always add a bias so that if my exponent is negative, I can make it positive by adding bias. Now, where did this plus 127 come from? I told you that an exponent is represented in 8 bits, so 8 bits. What does 8 bits mean? I can represent it from 0 to 255 characters. How? 2 to the power of 8 is 256, right? That means from 0 to 255, right? I can represent the characters in this range. Now I want to take a center point of this, which is, let's say, plus 1 to 7, okay? And use it as a bias. What I mean to say is, what I have to do is, because negative numbers can also come here, because in this, negative or positive numbers can also come in the exponent. I want to make every number an unsigned number. Unsigned means move towards a positive number. So that the calculation in the processor becomes easy. That's why I make the bias of 127. So how did I make the formula of this bias? Whatever is the range of the exponent, like here it is 8 bits. So 2 to the power of 8 minus 1, minus 1. So what will come here? 2 to the power of 7 minus 1. So what is 2 to the power of 7? Sorry, 2 to the power of 7 is 128. If we do minus 1, then our bias will be 127. Okay. I hope you understood this. Now, there can be another question in this. Why did we take bias as 127? We could have taken it as 128 also. But again, its question will go into more technical and microprocessor depth. That how calculations are done in it. Okay. We don't need to go into that much depth in this particular case. Okay. We can just understand that choosing 127 as a bias makes calculation very fast. And that's why its formula also became. If you have to calculate tomorrow that how much bias will be there. So, you can store all the exponents. Minus 1, minus overall 1. So, whatever will come, will come out. What should be your bias? I hope you understood this. Now, let's move towards our last step. And let's see. We don't have floating point numbers. We also have double. Means, we can also write this. Double D equal to, let's say, 3, 2, 0.4, 1, 5, 6, anything. We can also do double representation in memory. So, how double is represented in memory? So, this Like we did with float, if you understand that, you will understand this too. Just remember that in double, we don't use 32 bit, we use 64 bit. So this becomes our 64 bit representation. Now if it is 64 bit representation then obviously here 1 bit will be your sign bit again And here will be your exponent and here will be your mantissa Now see in floating because 32 bit was total so our exponent was 8 bit Here our exponent becomes 11 bit And remaining remaining how much is left 52 bits so our 52 bits mantissa becomes Rest everything will remain the same. If you want to store this, then you have to remove the binary of 32 first and then its binary. And then you have to bring it in the form of 1.xx into 2 to the power exponent. In this case, what will be the bias? Now see here the exponent is of 11 bytes. So 2 to the power of 11 minus 1 minus 1. which means 2 to the power of 10 minus 1. 2 to the power of 10 is 1024 and minus 1 is 1023. So 1023 will be your bias for double. So this is the calculation. Now you can also find out how a number is stored in memory for double and float. Generally, a question is asked for float because double is very big to express. And the interesting thing is that now you must be understanding that both of them, whether they are float even if it is double, we will not be able to store any number exactly. What is the reason for that? Because any number, like we took the example of 0.7, here 7 will never be able to be converted exactly in binary and it will keep on running infinitely. So in that case, we will always try to give an estimated value of float or double. That's why Java concept has brought a new number, a new method which we called big decimal. And this always gives you the exact value. How does it give this? When we understand big decibels, then we will study. If you put 0.7 in memory and asked for output, then you will always get 0.7. You will never get estimated value. We will study about this in future videos. When we will move from primitive data types to non-primitive data types. That's all for today's video. I hope you enjoyed it. Practice all this. See you in the next video. Till then, thank you so much.