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*

Have you tried "dividing by eight"?

Henry

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*

But how many bytes is 68 bits?

Tim Driven Development

`byte`(the Java datatype).

It's simply

`bits/8`(as you did)...however, that is integer division.

To see what I mean, use your version 1 code and enter (as suggested above) 68, instead of 64.

The proper answer is 8.5, but what do you get with your code?

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*

Dave Tolls wrote:It's nothing to do with

byte(the Java datatype).

The proper answer is 8.5, but what do you get with your code?

Yup, I get 8.0 bytes but the book from the question is 64, not 68. That confusing me

*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.

*Learning language is easy but learning basics is difficult*

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 value-1 (7) to the count and divide by the max value(8): (3+7)/8 = 1

How come 68 / 8 I will get 8.0 instead of 8.5 ?

*Learning language is easy but learning basics is difficult*

Norm Radder wrote:

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?

0.0

*Learning language is easy but learning basics is difficult*

Norm Radder wrote:

A solution for rounding when working with integer division is to add the max value-1 (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 requireJohn Joe wrote:

Tim Cooke wrote:. . . But how many bytes is 68 bits?

I get 8.0 bytes . . . .

__9__bytes to record 68 bits. You are still using integer arithmetic inappropriately.

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

Tim Driven Development

Can you please copy/paste instructions exactly as they appear?

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.

Liutauras Vilda wrote:@OP

Can you please copy/paste instructions exactly as they appear?

My question on here is the question from the Programming book.

*Learning language is easy but learning basics is difficult*

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.

Liutauras Vilda wrote:

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.

Yeah...still have more to go.

*Learning language is easy but learning basics is difficult*

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |