Christian Weiß
/
BitShiftInC
Revision 0:146703915683, committed 2018-11-15
- Comitter:
- Wizo
- Date:
- Thu Nov 15 17:52:30 2018 +0000
- Commit message:
- BitShiftInC
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Nov 15 17:52:30 2018 +0000 @@ -0,0 +1,73 @@ +// Bitoperationen in C +// & | ^ << >> + +// Logische Verknüpfungs-Operatoren +// && || + + +// Der Bitschiebe-Befehl in C/C++ + +int a; + +a = B#0001; // in a steht Binär 0001 + +a = a << 1; // in a steht jetzt B#0010 +a = a << 1; // in a steht jetzt B#0100 + +a = a >> 2; // in a steht jetzt B#0001 +a = a >> 1; // in a steht jetzt B#0000 + + +// Bitwise OR + +B#1001 | B#0010 = B#1011 + + +// Bits in einem Byte mit Bitwise-And abfragen + +B#1010 & B#0101 = B#0000 + +B#1010 & B#1000 = B#1000 + +// !!gezieltes!! abfragen ob ein Bit in einem Byte gesetzt ist + + 2^3 2^2 2^1 2^0 +B# 0 0 0 0; + +int btn; + +if( btn & 1 ) B#xxxx & B#0001 + printf("Bit0 ist gesetzt" + +if( btn & 2 ) B#xxxx & B#0010 + printf("Bit1 ist gesetzt"); + +if( btn & 4 ) + printf("Bit2 ist gesetzt"); + +if( btn & 8 ) + printf("Bit3 ist gesetzt"); + + +// Mit dem Bitwise-OR Bits in einem Byte setzen +// ohne die schon gesetzen Bits zu zerstören + +int leds; + +leds = B#0001; // Bit0 ist gesetzt + +leds = B#0010; // Bit1 ist gesetzt aber Bit0 ist leider wieder gelöscht + +// Die Lösung ist, daß man Bits dazuverodert +leds = leds | B#0010; // leds ist jetzt B#0011; + + + +// Logische operatoren + +int a = 1; +int b = 1; + +int c = ( a && b ); + +int d = 5; // B#0101 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 15 17:52:30 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/5aab5a7997ee \ No newline at end of file