I am doing an online course and for one of the chapters (in Practical C++ Programming by Steve Oualline) it had a bitmapped graphics section-only 4 pages long. The chapter is about bit operations and includes a section about hexadecimals. I understoodit until I got to this passage in bitmapped graphics:

"Suppose we have a small graphics devicce- a 16 by 16 pixel monochrome display. We want to set a bit at 4,7. But we have a problem. There is no data type for an array of bits in C++. The closest we can come is an array of bytes. Our 16 by 16 array of bits now becomes a 2 by 16 array of bytes. [[I]Why isn't it a 2 by 2 array?[/I]] To set the pixel at bit number 4,7 we need to set the fourth bit of byte (0,7). To set this bit we would use the statement bit_array[0][7] |= (0x80>>(4));"

What was the |= in the statement and I am also in need of a step by step explanation of the bit_array[0][7] |= (0x80>>(4)); statement (dont tell me it was explained above-it didnt make sense to me). Also, how would you output a simple 8 bit by 8 bit graphics that has 1 pixel turned on the rest off(because the book has no explanation of how to output that to the console)?
Thank You for your help.

Recommended Answers

All 10 Replies

| is the bitwise OR operator it means you are performing an OR on 1 bit(0 and 1). This is in contrast with the logical OR operator || which performs his actions on true and false values(booleans)

>> is a bitwise right shift operator.
In your case the hex value 0x80 gets shifted 4 bit-positions to the right.

Hope this helps.

Our 16 by 16 array of bits now becomes a 2 by 16 array of bytes. [[I]Why isn't it a 2 by 2 array?[/I]]

A byte is made up of 8 bits. Therefore a row can contain the 16 bits in 2 bytes. There are 16 rows, hence 2 x 16 bytes.

Calculate 16 x 16 bits = (2 x 8) x 16 bits = 256 bits.

Still how would you output the bitmap? And why is there an = sign after the |?

a |= b; is the same as a = a | b; It is a sort of shorthand notation. Just as a += b; is the same as a = a + b; It is probably invented to make life of newbies a little harder...

commented: He was helpful in revealing a simple thing to a newbie +1

Oh, wow....I don't know how i didn't see that. Still, how would you output the bitmap and why is the book using hexadecimals-is it really neccessary (can you use just binary)?

Well... hexadecimal is a shorthand for binary, but a very usefull one!
Google hexadecimal to find out more!

I did and I found an article at http://www.codeproject.com/KB/cpp/bitbashing.aspx that explained how a hexadecimal is like a nibble and very useful in representng binary but would it be possible to say bit_array[0][4]=bin 0001 or something like that and do you know of any sites that explain outputing "manual" bitmaps (not pictures)?

thank you for your help

What do you mean by "manual" bitmaps?
Do you mean to output zeros and ones instead of pixels?

Usually when I google bitmaps I get how to upload .bmp's but I want to manualy set the pixels in say a 16by16 bit bitmap (3,6)=1 (2,9)=1...
I looked at the sample program in teh book and it just outputs "." do you know if you could actually make a bitmap in C++.

I have found this on http://www.java2s.com/Tutorial/Cpp/CatalogCpp.htm

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

int main()
{
   unsigned value = 123;
   const int SHIFT = 8 * sizeof( unsigned ) - 1;
   const unsigned MASK = 1 << SHIFT;

   for ( int i = 1; i <= SHIFT + 1; i++ ) 
   {
      cout << ( value & MASK ? '1' : '0' );
      value <<= 1;

      if ( i  8 == 0 )
         cout << ' ';
   }

   cout << endl;
   return 0;
}

the output is :
00000000 00000000 00000000 01111011

Does this help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.