Question : Write a program reads number in bits,convert to bytes, and display the result. One byte is 8 bits.
The output should be
Enter the number of bits : 64
64 bits is 8.0 bytes
This is what I've tried
But I get wrong output. Then I try another way
Unfortunately, no luck too.
Learning language is easy but learning basics is difficult
Something to think about: How many bytes is 68 bits? What should the output be in this case?
Tim Driven Development
Now what confused you are probably bits bytes and so forth which you interpreted them as implementation details.
1. What you reading in, is a text and nothing else. Now change that approach slightly to read in number, an integer of type int or other numeric type which has more precision. Scanner has method for that  research in Java API documentation.
2. Do some math with it.
3. Print out. Done.
Solution 1
Solution 2
Since the output want the bytes in double value (8.0), so in Solution 1 I use the double datatype, but this did not converts to byte.
Solution 2 I use byte, but I cannot get the double value for the bytes.
Learning language is easy but learning basics is difficult
Also work out which datatype each of the divisions is. Remember that the byte datatype is a red herring.
Neither version of the program will give the correct answer for 68.
Tim Cooke wrote:As I mentioned before, I don't think you need the byte Java type at all. Solution 1 is what I had in mind.
But how many bytes is 68 bits?
I get 8.0 bytes .
But I follow the question from Java Programming books, the input given is 64.
Learning language is easy but learning basics is difficult
 1
What you get is a whole number in simplified terms, even tho your result is shown in decimal number.
Now, in order to get precise result for the case/s been mentioned multiple times  you need to do division where one or both divisors are of type double, so you'd get actual result more precise.
When you divide integers:
4 / 2 you get 2
5 / 2 you get still 2 (as you are dealing with int data types)
now if you change one of those to a double type, what you get is:
5 / 2.0 or 5.0 / 2 or 5.0 / 2.0 is 2.5
So you need to follow latter approach attacking this problem.
simple mean divide the input by 8,
That works if the number of bits divides evenly by 8 like 64 does. But what if it doesn't? For example how many bytes for 3 bits?
A solution for rounding when working with integer division is to add the max value1 (7) to the count and divide by the max value(8): (3+7)/8 = 1
Norm Radder wrote:
A solution for rounding when working with integer division is to add the max value1 (7) to the count and divide by the max value(8): (3+7)/8 = 1
Thanks for pointing out the problem and giving a perfect answer !
Learning language is easy but learning basics is difficult
Tim Driven Development
I am not sure that smilie is correct; you would require 9 bytes to record 68 bits. You are still using integer arithmetic inappropriately.John Joe wrote:
Tim Cooke wrote:. . . But how many bytes is 68 bits?
I get 8.0 bytes . . . .
This assumes there are eight bits to a byte; a long time ago computers had six bits to the byte. We'll stick with eight.
But you can't record 7 bits in 0 bytes. You would have to use one byte, but only partially occupied. Similarly I think 15 bits would require two bytes, again one only partially occupied.Tim Cooke wrote:. . . Anything less than 8 bits is not a byte by my understanding.
Careful; OP is having enough difficulty dividing by 8 at the best of times. We shall confuse him by disagreeing about how many bytes you need for 7 bits.
The question we have all been discussing so far is: how many bytes can you fit in x bits?
Campbell asks: how many bytes do you need to represent x bits.
Given that byes don't divide I'd say the first question is the one being asked here. But maybe I'm wrong?
Tim Driven Development
Since the OP isn't precise (as Liutauras noted) about which version is wanted, both are right and both are wrong.Tim Cooke wrote:. . . how many bytes can you fit in x bits? . . . how many bytes do you need to represent x bits. . . .
I think a better solution is to get out of the integer arithmetic; then you can simply implement both versions.
Fortunately we are dividing small whole numbers by 8: that means the quotients can be exactly represented in binary IEEE754 format.
Campbell Ritchie wrote:That doesn't help us. We ought to know which book to prevent copyright problems, and because some of us might have the same book. We also need to know exactly what you want. Is 68 bits 8 bytes or 8.0 bytes or 8½ bytes or 8.5 bytes or 9 bytes?
I refer to this book "Introduction to Java Programming"
The question is in Chapter 2 Programming Exercise , Question 2.3.
Learning language is easy but learning basics is difficult
Campbell Ritchie wrote:That would appear to be Liang's book. What does it say that would help with the 68‑bit question?
It didn't said anything. It just ask us to write the program by following the instruction given and also a sample to run (output).
Learning language is easy but learning basics is difficult
John Joe wrote:
Campbell Ritchie wrote:That would appear to be Liang's book. What does it say that would help with the 68‑bit question?
It didn't said anything. It just ask us to write the program by following the instruction given and also a sample to run (output).
Probably we'll need to leave it here then by making assumption as Campbell pointed out  both answers provided above are correct and wrong at the same time as long as we have as much details provided as we do now.
I think you got some other chapters to work on > 1300 pages You hardly finish this in 2017, what we have now, mid of March already, don't we.
it's a teeny, tiny, wafer thin ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJIDEA
