Codeschnipsel

Aus TippvomTibb
Zur Navigation springen Zur Suche springen

Bitmanipulation

Setzen A |= (1 << n)

A |= Bit-Maske

  1. define SET_BIT(byte, bit) ((byte) |= (1UL << (bit)))

Loeschen A &= ~(1 << n)

A &= ~(Bit-Maske)

  1. define CLEAR_BIT(byte,bit) ((byte) &= ~(1UL << (bit)))


Toggle A ^= (1 << n)

A ^= (Bit-Maske)

  1. define TOGGLE_BIT(byte,bit) ((byte) ^= (1UL << (bit)))


Pruefen A &= (1 << n)

  1. define IS_SET(byte,bit) (((byte) & (1UL << (bit))) >> (bit))


(s)printf-Ersatz in AVR

 sprintf(buffer,"Zaehlerstand: %09.3f",zaehlerStand); not supported in avr-gcc; grrrr!

Workaround 1:

dtostrf(zaehlerStand,9,3,zaehlerStandStr); // keine fuehrenden Nullen; grrrr!
sprintf(buffer,"%s",zaehlerStandStr);

Workaround 2:

 sprintf(buffer,"%05d.%03d",(int)zaehlerStand,(int)(zaehlerStand*1000)%1000);