Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Convert bits to byte

Ranch Hand
Posts: 547
3
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.

author
Sheriff
Posts: 23507
138

Have you tried "dividing by eight"?

Henry

Marshal
Posts: 4407
281
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?

Sheriff
Posts: 5691
393
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.

John Joe
Ranch Hand
Posts: 547
3
Thanks guys for the  reply. I tried another two ways and finally get the results. Which solution meet the question needs?

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.

Tim Cooke
Marshal
Posts: 4407
281
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?

Rancher
Posts: 3493
39
It's nothing to do with 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?

Marshal
Posts: 59101
180
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.

John Joe
Ranch Hand
Posts: 547
3

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.

John Joe
Ranch Hand
Posts: 547
3

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

Liutauras Vilda
Sheriff
Posts: 5691
393
• 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.

John Joe
Ranch Hand
Posts: 547
3
Thanks guys, I think I misleading the question and think over complicated. The question stated "convert to bytes" just simple mean divide the input by 8, not converting to datatype byte.

Rancher
Posts: 3066
33

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

John Joe
Ranch Hand
Posts: 547
3

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

John Joe
Ranch Hand
Posts: 547
3

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

John Joe
Ranch Hand
Posts: 547
3

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 !

Tim Cooke
Marshal
Posts: 4407
281
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.

Campbell Ritchie
Marshal
Posts: 59101
180

John Joe wrote:

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

I get 8.0 bytes .    . . .

I am not sure that smilie is correct; you would require 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: 59101
180

Tim Cooke wrote:. . . Anything less than 8 bits is not a byte by my understanding.

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.

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.

Tim Cooke
Marshal
Posts: 4407
281
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?

Tim Cooke
Marshal
Posts: 4407
281
It's also worth noting that it's Campbell and not I who writes programming languages for a living.

Liutauras Vilda
Sheriff
Posts: 5691
393
@OP

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

Campbell Ritchie
Marshal
Posts: 59101
180

Tim Cooke wrote:. . . how many bytes can you fit in x bits?  . . . how many bytes do you need to represent x bits.  . . .

Since the OP isn't precise (as Liutauras noted) about which version is wanted, both are right and both are wrong.

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.

John Joe
Ranch Hand
Posts: 547
3

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.

Campbell Ritchie
Marshal
Posts: 59101
180
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?

John Joe
Ranch Hand
Posts: 547
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.

Campbell Ritchie
Marshal
Posts: 59101
180
That would appear to be Liang's book. What does it say that would help with the 68‑bit question?

John Joe
Ranch Hand
Posts: 547
3

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

Liutauras Vilda
Sheriff
Posts: 5691
393

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.

John Joe
Ranch Hand
Posts: 547
3

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.

 it's a teeny, tiny, wafer thin ad: Why should you try IntelliJ IDEA ? https://coderanch.com/wiki/696337/IntelliJ-IDEA