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.

Dani AI

Generated

Short expert summary and a concrete example that fills gaps from the replies by and .

The book’s layout (a 16×16 monochrome display stored as “2 by 16 bytes”) means each row is 16 bits → 2 bytes, and those two bytes are indexed per row. For a pixel at (x,y): compute the byte column as x/8 and the bit offset as x%8. The expression in the book

bit_array[0][7] |= (0x80 >> (4));

breaks down like this:

  • 0x80 is binary 1000 0000.
  • 0x80 >> 4 becomes 0000 1000 (hex 0x08) — the mask that targets the column within that byte.
  • bit_array[0][7] |= mask sets that mask bit while leaving other bits in the same byte unchanged (same as bit_array[0][7] = bit_array[0][7] | mask). This is MSB-first bit ordering; if a project uses LSB-first, the mask would be 1 << (x % 8) instead.

A minimal, practical C++ sketch that stores a 16×16 monochrome buffer, turns one pixel on, and prints a text grid:

#include <iostream>
#include <array>
#include <cstdint>

const int W = 16, H = 16;
using Row = std::array<uint8_t, W/8>;
Row bitmap[H] = {};

void setPixel(int x, int y) {
  if (x<0 || x>=W || y<0 || y>=H) return;
  bitmap[y][x/8] |= static_cast<uint8_t>(0x80 >> (x % 8)); // MSB-first
}

void printBitmap() {
  for (int y=0;y<H;++y) {
    for (int x=0;x<W;++x)
      std::cout << ((bitmap[y][x/8] & (0x80 >> (x % 8))) ? 'X' : '.');
    std::cout << '\n';
  }
}

int main() {
  setPixel(4,7);
  printBitmap();
}

Notes and cautions:

  • Hex is used because each nibble = 4 bits, so hex is compact for byte-sized masks. Older C++ had no binary literal syntax; modern toolchains accept 0b... or std::bitset for clearer intent.
  • When exporting an actual image file (BMP/PNG) the console-print approach is only for debugging; writing a real bitmap requires proper headers or a small image library.
  • Always document whether bytes use MSB-first or LSB-first bit ordering so other code and file formats remain consistent.

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