Uebungen
Bitmanipulationen
- include "mbed.h"
BusOut myleds(LED1, LED2, LED3, LED4);
void print_binary(uint8_t x) { function to print binary
for(int i=sizeof(x)<<3; i; i)
putchar('0'+((x>>(i-1))&1));
}
int main() { uint8_t value = 0b01010110; C++14 Standard Extension uint8_t mask = 0b00001111; mask lower nibble uint8_t position = 0; for later use is position of bit in mask
scanf("%d", &value); mask=1<<position;
printf("value : "); print_binary(value); call function print_binary printf("\nmask : "); print_binary(mask);
printf("\nLoeschen: \n "); value=value& mask; myleds = value; print_binary(myleds.read());
value = 0b01010110; value=value|mask; myleds = value;
printf("\nSetzen: \n"); print_binary(myleds.read());
value = 0b01010110; printf("\n\nToogle\n");
value=value^mask; print_binary(value);
value>>=position; if((value&1)!=0) printf("\nresult: true"); else printf("\n false");
printf("\n\nshift left: %X\n", value<<1); printf("shift right: %X\n", value>>1);
}