3

I am a newbie! I need to pass an array of hex values bit by bit. I am not sure as to how to do it.

let's say I have array defined below,

const uint_8t a[] = { 0xAA, 0xF8, 0x03, ... };
int size=sizeof(a) / 8;

//edited

SBIT(port, SFR_P0, 1);

void Data(int *a, int size) {   
    int i;

    for (i = 0; i < size; i++) {
        port = a[i];
    }
}

How can I pass this array bit by bit to a function pointing to MSB first.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
sss
  • 41
  • 5

2 Answers2

1

You cannot pass bit addresses. But you can enumerate the bits and pass their values with 2 nested loops:

const uint_8t a[] = { 0xAA, 0xF8, 0x03, ... };
size_t size = sizeof(a);
for (size_t i = 0; i < size; i++) {
    for (int shift = 8; shift-- > 0; ) {
        transmit_bit((a[i] >> shift) & 1);
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I believe the inner loop should be `for (int shift = 7; shift-- >= 0; )` as otherwise the most significant bit is shifted out in the first iteration. – Codor Feb 18 '16 at 08:40
  • 2
    @Codor: no, your version omits the most significant bit. An alternative that requires a signed type would be `for (int shift = 7; shift >= 0; shift--) ...` but I like to emphasize the number of iterations `8`, and using `shift-- > 0` allows an unsigned type to enumerate values from `8-1` to `0` inclusively. – chqrlie Feb 18 '16 at 08:44
  • You're right, I had a misconception on when the `shift--` takes place. – Codor Feb 18 '16 at 08:46
  • Thank you. But, by doing a[i]>>shift, I am doing right shift and passing LSB first right? – sss Feb 18 '16 at 08:49
  • 1
    @Codor: an alternative that is slightly more readable is `for (int shift = 8; --shift >= 0; )` but it can only be used for signed types. I use the posted one for most loops with a decrementing counter to avoid having to worry about signedness. – chqrlie Feb 18 '16 at 08:50
  • @sss: no the first bit transmitted is `a[0] >> 7`, the second is `(a[0] >> 6) & 1`... the bits are transmitted as binary values `0` or `1` from the most significant to the least significant. If you meant to transmit bytes, that's a different problem. And if you meant to transmit `0x80` for the most significant bit set, it is easy to change the expression to `transmit_bit((a[i] & (1 << shift));` – chqrlie Feb 18 '16 at 08:51
  • @sss In the shift you describe, it depends an the value of `shift`. – Codor Feb 18 '16 at 08:52
  • @chqrlie Thanks. Solved my problem – sss Feb 18 '16 at 08:54
  • So, I have edited as port=((a[i]>>shift)&1) shift I have defined as 8. This is correct I suppose – sss Feb 18 '16 at 09:03
  • @sss: I suppose `port` is supposed to be assigned a value `0` or `1`. You have a reputation of 21 now. – chqrlie Feb 18 '16 at 10:12
0

The individual bits of a single uint_8t arg can be indexed as follows, where the most significant bit is processed first.

for (int i = 7; i >= 0; i--)
{
    uint_8t i_th_bit = ( arg >> i ) & 1;
}

You could iterate over a and process the bytes bit-wise as above.

Codor
  • 17,447
  • 9
  • 29
  • 56