views:

111

answers:

2

Hi Guys,

This was asked in one of the interviews and i cant seem to find any clue about it. The question is:

I have 40 x 20 screen, and i want to store these pixels in a byte array so that i can reconstruct this screen again from the byte array. The size of byte array is 100bytes.

Another way of looking at it is. How do we store a single pixel (x,y) using one bit. Since, there are 40 * 20 = 800 pixels and we have 100 bytes.

Any hints/ideas or reference links will be helpful

Thanks, Chander

+1  A: 

I think the word you're looking for is bitmap. Each bit in the 80 bytes = 1 pixel on the screen (0 = black, 1 = white).

You don't need more than that because the shape of the output is a given.

egrunin
+1  A: 

One byte is 8 bit. So you can store 100 byte · 8 bit/byte = 800 bit of information with these 100 byte.

As each bit can have two values (0, 1), you can only represent two states with each bit. In case of a screen, these two states could be 0 = black and 1 = white or 0 = light off and 1 = light on.

And as you have 800 bit, you can represent your 40 · 20 pixel = 800 pixel with these 800 bits, with each bit represents one of the two states.

Gumbo