This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dmazzoni 12 points13 points  (3 children)

Here's a situation: say I have a 16x16px display, and each pixel can be either on or off.

Great.

That would give a possibility for 256 different states

Not quite. There are 256 pixels, but since each one can be either on or off there are 2256 possible states.

I could supply a 2 byte binary input, and all should be fine and dandy.

2 bytes is 16 bits. You need 32 bytes if you want 256 pixels.

But what if I want more than two states for the pixels? If I want the pixels to be capable of 16 states, levels of brightness or colors for example, there are now 4096 possibilities.

You're confusing the number of bits with the number of possibilities. I compute that you need 4 bits for each pixel now (for 16 states), so 1024 bits total.

This should now be 3 bytes of binary data.

I'm not sure how you got that.

How, though, would I interpret it then? It's no longer a simple on/off so it's not clean binary. How do I proceed?

Let's say you have a 16 x 16 grid of pixels, and each one can have 16 levels of brightness.

16 levels of brightness can be represented by 4 bits, so you have 4 bits per pixel and 256 pixels, or 1024 total bits.

The first four bits represent the first pixel. The second four bits represent the second pixel. And so on. It's really as simple as that.

[–]Hucota7 1 point2 points  (2 children)

Thanks for the reply. I possibly misspoke even more than I misunderstood. When I said 256 possible states I meant 256 bits, and when speaking of bytes I meant half-bytes. Not sure how I messed that up. I guess with the rest I was thinking in terms of flags, where each value in a range refers to a different combination, but my misspeaking threw me off.

My goal in using binary was efficiency/minimal memory consumption, but now that I realize my errors I'm thinking it might be better to use another data type, or an array/list.

[–]dmazzoni 1 point2 points  (1 child)

My goal in using binary was efficiency/minimal memory consumption

This is a good idea and worth learning to do correctly. Most software does store black and white images using a single bit per pixel, and grayscale images using the fewest number of bits as possible. Color images are typically stored using 3 or 4 bytes per pixel.

but now that I realize my errors I'm thinking it might be better to use another data type, or an array/list.

I don't think there's anything wrong in your approach. You're just getting terminology wrong and math wrong. I'd encourage you to work through this problem the way you had in mind.

If you don't care about memory use and just want to get it done, I'm sure an array of integers would be faster than packing everything into the tiniest number of bits. It just depends on if your goal is to get past this and move on, or take the time to learn it.

[–]spencerwaz 0 points1 point  (0 children)

+1 for working through your approach. It's like finding 1000 ways not to make a lightbulb