Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
You are viewing an older revision! See the latest version
Bitmanipulationen Grundlegend
Kopiere das Programm in den mbed-Simulator und ändere die globale Variable shift und kontrolliere die vier blauen Leds.
bit-shift.cpp
#include "mbed.h" DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); uint8_t pos = 0; uint8_t shift = 3; // change the value between 0 and 3 int main() { while (1) { pos = (1 << shift); // shift left printf("%d\n", pos); if(pos == 0b00001000) led4 = !led4; if(pos == 0b00000100) led3 = !led3; if(pos == 0b00000010) led2 = !led2; if(pos == 0b00000001) led1 = !led1; wait_ms(500); } }
Maskierung
Baue den folgenden Code in ein Programm ein und verwende an Stelle der konstanten Werte Variable.
uint8_t mask = 0x0f; //00001111b uint8_t value = 0x55; //01010101b value = mask & value;
Beispiel für das Extrahieren der 4 Bytes aus einer 32 Bit Integer Zahl (& und >>):
uint32_t value = 0x01020304 //Example value uint32_t byte1 = (value >> 24); //0x01020304 >> 24 is 0x01 so //no masking is necessary uint32_t byte2 = (value >> 16) & 0xff; //0x01020304 >> 16 is 0x0102 so //we must mask to get 0x02 uint32_t byte3 = (value >> 8) & 0xff; //0x01020304 >> 8 is 0x010203 so //we must mask to get 0x03 uint32_t byte4 = value & 0xff; //here we only mask, no shifting