Und hier noch mal in C:
Code:
uint8_t bitswp2_4_in_c(uint8_t byte) {
uint8_t anded=byte&(uint8_t)0x14;
if (anded==(uint8_t)0x14 || !anded) return byte; // bits identisch, nix ändern!
return byte^0x14; // bits invertieren
}
Mit Optimierung "-O1" sieht das so kurz aus:
Code:
47: uint8_t bitswp2_4_in_c(uint8_t byte) {
+000000DD: 2F98 MOV R25,R24 Copy register
48: uint8_t anded=byte&0x14;
+000000DE: 7184 ANDI R24,0x14 Logical AND with immediate
49: if (anded==(uint8_t)0x14 || !anded) return byte;
+000000DF: 3184 CPI R24,0x14 Compare with immediate
+000000E0: F021 BREQ PC+0x05 Branch if equal
+000000E1: 2388 TST R24 Test for Zero or Minus
+000000E2: F011 BREQ PC+0x03 Branch if equal
50: return byte^0x14;
+000000E3: E184 LDI R24,0x14 Load immediate
+000000E4: 2798 EOR R25,R24 Exclusive OR
51: }
+000000E5: 2F89 MOV R24,R25 Copy register
+000000E6: 9508 RET
Eleganter kann ich's nicht.
Lesezeichen