posted 8 months ago

Hi Ranches, can someone help me to solve this problem ?

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.

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*

posted 8 months ago

It sounds like some simple arithmetic is all that's required here. Take the example: 64 bits is 8 bytes. It's just 64 divided by 8. No need to use the byte Java type at all. In fact I think it's misleading you.

Something to think about: How many bytes is 68 bits? What should the output be in this case?

Something to think about: How many bytes is 68 bits? What should the output be in this case?

Tim Driven Development

posted 8 months ago

Hi, it seems to me you over complicated this problem.

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.

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.

posted 8 months ago

Thanks guys for the reply. I tried another two ways and finally get the results. Which solution meet the question needs?

Since the output want the bytes in double value (8.0), so in

Solution 2 I use byte, but I cannot get the double value for the bytes.

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

Dave Tolls

Ranch Foreman

Posts: 2996

37

Campbell Ritchie

Marshal

Posts: 56197

171

posted 8 months ago

Turrn your computer off. Write down how you are going to do it without a computer. And work out what you are going to do with 68 bits, as Tim asked earlier.

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.

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.

posted 8 months ago

I get 8.0 bytes .

But I follow the question from Java Programming books, the input given is 64.

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*

posted 8 months ago

- 1

You miss the point what you are being told.

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.

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.

Norm Radder

Rancher

Posts: 2240

28

posted 8 months ago

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

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

posted 8 months ago

Thanks for pointing out the problem and giving a perfect answer !

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*

posted 8 months ago

This is what I was getting at and is more a question of requirement. Is there such a thing as a fraction of a byte? We've established 8 bits is 1 byte, but what about 9 bits? 10 bits? 15 bits? I say all those are still 1 byte. Anything less than 8 bits is not a byte by my understanding.

Tim Driven Development

Campbell Ritchie

Marshal

Posts: 56197

171

posted 8 months ago

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.

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.

Campbell Ritchie

Marshal

Posts: 56197

171

posted 8 months ago

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.

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.

posted 8 months ago

Now Campbell has introduced a second interpretation of the question. How interesting!

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?

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

Campbell Ritchie

Marshal

Posts: 56197

171

posted 8 months ago

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.

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

Marshal

Posts: 56197

171

posted 8 months ago

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?

posted 8 months ago

I refer to this book "Introduction to Java Programming"

The question is in Chapter 2 Programming Exercise , Question 2.3.

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

Marshal

Posts: 56197

171

posted 8 months ago

That would appear to be Liang's book. What does it say that would help with the 68‑bit question?

posted 8 months ago

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

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*

posted 8 months ago

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.

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.