-1

I am trying to get to read individual bits of a byte array. I am basically iterating through the byte array and want to tell whether each individual bit is 0 or 1.

My problem is, I am unable to differentiate between a 0 and 1 bit. The code is always reading each bit as a 1.

This is my code:

const unsigned char bitmap[] = {
    0x00,0xFF,0xFF,0x00,0x00,0x00,
    0x00,0x00,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF
};

void drawBitmap(Framebuffer *fb) {

    uint8_t x = 1;
    for (int i = 0; i < sizeof(bitmap); ++i) {
        for (int p = 0; p < 8; ++p) {
            if ((bitmap[i] >> p) & 1) { // If bit
                fb->drawPixel(x, 1); // **RIGHT HERE** --> I AM ALWAYS GETTING THIS AS TRUE 
            }
            x++;
        }
    }
}

Note that there are some bytes that should be all zeroes (0x00). I am assuming by default these are bytes (8 bits), right? Any ideas why am I unable to differentiate between a 1 and a 0?

Note: Here's the whole code... I am trying to use this library: https://github.com/tibounise/SSD1306-AVR with an atmega328P... This just doesn't make any sensse. Whenever I use "fb->drawPixel(x, 1);" on it's own it works fine, but on that particular function I always get a "1" (a pixel).

#define F_CPU 14745600

#include <stdint.h>
#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include <util/delay.h>

//#include "SSD1306.h"
#include "Framebuffer.h"

const unsigned char bitmap[] = {
    0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF
};


void drawBitmap(Framebuffer *fb) {

    uint8_t x = 1;
    int z = 0;
    for (int i = 0; i < sizeof(bitmap); ++i) {
        for (int p = 0; p < 8; ++p) {
            if ((bitmap[i] >> p) & 1) { // If bit
                fb->drawPixel(x,1);             
            }
            x++;
        }
    }

}


int main(void) {


    //const uint8_t *bitmap;
    //bitmap = &bitmap1;

    Framebuffer fb;
    Framebuffer *FB;

    //Pointer to the class
    FB = &fb;

    //fb.drawPixel(5, 5);

    drawBitmap(FB);

    fb.show();

    //delay_ms(1000);


    return 0;
}

Any ideas?

Thanks in advance.

Luis Cruz
  • 1,488
  • 3
  • 22
  • 50
  • Possible duplicate of [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – DeiDei Feb 10 '18 at 07:02
  • Works here: https://ideone.com/vh4itX – Barmar Feb 10 '18 at 07:02
  • It works...maybe you are showing the wrong code? – user2736738 Feb 10 '18 at 07:03
  • Are you sure you cleared your framebuffer before calling the function? You might be seeing bits left over from before. – Barmar Feb 10 '18 at 07:03
  • My prime suspect is the code you do not show. Can you make a [mcve] using prints instead of graphics. I.e. please prove that what you feed into the representation is always true. – Yunnosch Feb 10 '18 at 07:39
  • I posted the whole code... Still, there is no way other part of the code is affecting that function... I just don't get it, this doesn't make any sense... – Luis Cruz Feb 10 '18 at 08:18
  • Use a debugger to follow the program flow – M.M Feb 10 '18 at 10:32
  • @LuisCruz Your `bitmap` is a 16-byte array made up of bytes `0` and `255`, either white or black no in-between. So for any byte, you will get all zeros or all ones, no other outcome is possible with your data. – David C. Rankin Feb 11 '18 at 08:23

1 Answers1

1

Your code seems okay. However your sample data is in bytes, not bits.

If you are working with a 1-bit bitmap, there are 8-bits per pixel. Each bit is 0 or 1 (black or white). Each set of 8-bits is compacted in to byte:

0xFF contains 8-bits: "11111111"
0x00 contains 8-bits: "00000000"

An actual bitmap may have the following sequence:

"1011 0001"

Try the same code with a more realistic data:

const unsigned char bitmap[] = 
{
    1 | 0 | 4 | 8 | 0 | 0 | 0 | 128,//1011 0001 <- first 8-bits
    0xFF, //<- second set of 8-bits
    0x00, //...
};

int main(void)
{
    for(int i = 0; i < sizeof(bitmap); ++i)
    {
        printf("%3d ", bitmap[i]);
        for(int p = 0; p < 8; ++p)
            printf(((bitmap[i] >> p) & 1) ? "1" : "0");
        printf("\n");
    }

    return 0;
}

Result:

141 10110001
255 11111111
  0 00000000
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77