Hi
Ich habe ein Byte, in dem ich Bit 2 und 4 miteinander austauschen möchte.
Wie kann ich das am "elegantesten" tun?
gruß
Hi
Ich habe ein Byte, in dem ich Bit 2 und 4 miteinander austauschen möchte.
Wie kann ich das am "elegantesten" tun?
gruß
Hi!
Ich habs mal ausm Ärmel geschüttelt. (Also nicht getestet)
So müsste es gehen:
Ich ging mal davon aus dass du mit Bit 2 das Bit an zweiter stelle von rechts meintest. (LSB ist Bit1)Code:bla = (bla & 0xF5) | ((bla & 0x02)<<2) | ((bla & 0x08)>>2);
Wenn nicht, dann probiers damit: (LSB ist Bit0)
Ob das nun eine "elegante" Methode ist, darüber lässt sich sicher streiten.Code:bla = (bla & 0xE6) | ((bla & 0x04)<<2) | ((bla & 0x10)>>2);
Ich würds so machen...
Gruß
Hi,
ganz stur so (Bitzählung von 0 bis 7!):
Schnell hingeschrieben und ungetestet. Mal sehen, ob jemand eine kürzere Lösung findet! Anmerkung: "inttypes.h" muss eingebunden sein. Aufruf mitCode:void inline bitswp2_4(uint8_t b) { asm volatile ( "bst %0,2" "\n\t" "sbr %0,4" "\n\t" "sbrs %0,4" "\n\t" "cbr %0,4" "\n\t" "bld %0,4" "\n\t" : : "d" (b) ); }
"bitswp2_4(byte_argument)".
Gruß
Fred
Only entropy comes easy. - Anton Checkhov
Hier noch eine Zeile kürzer:
Code:void inline bitswp2_4_new(uint8_t b) { asm volatile ( "mov __tmp_reg__, %0" "\n\t" "bst %0,2" "\n\t" "bld %0,4" "\n\t" "bst __tmp_reg__,4" "\n\t" "bld %0,2" "\n\t" : : "d" (b) ); }
Only entropy comes easy. - Anton Checkhov
Also fhs. Deine Posts begeistern mich immer wieder. Starkes Stück Code!
Danke für die Blumen ... ich hoffe, der Code stimmt! Eigentlich stehe ich mit dem Inline-Assembler auf Kriegsfuß (bin sonst ein sehr friedlicher Typ), aber ab und zu ist der dann doch hilfreich. Überlege gerade, ob man's nicht noch kürzer machen könnte.
Only entropy comes easy. - Anton Checkhov
Und hier noch mal in C:
Mit Optimierung "-O1" sieht das so kurz aus: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 }
Eleganter kann ich's nicht.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
Only entropy comes easy. - Anton Checkhov
Ja, guter Ansatz. Nur schade, dass das mühsam errechnete Ergebnis so achtlos weggeworfen wird.Zitat von thewulf00
Besser:
Code:static inline void bitswp2_4(uint8_t *b) { asm volatile ( "mov __tmp_reg__, %0" "\n\t" "bst %0,2" "\n\t" "bld %0,4" "\n\t" "bst __tmp_reg__,4" "\n\t" "bld %0,2" "\n\t" : "=d" (*b) : "0" (*b) ); }
MfG
Stefan
Hallo Stefan,
oops, ja, natürlich -- danke für die Verbesserung!
Gruß
Fred
Only entropy comes easy. - Anton Checkhov
Hi
Danke für die schnelle und ausführliche Hilfe!
Hab es jetzt aber doch per Hardware gelöst.
Lesezeichen