Forum: Compiler & IDEs 8 bit Bitmanipulation casten, sonst 16 bit?


von Gast (Gast)


Lesenswert?

Ist das noch aktuell?

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_intpromote :

Why *does* the *compiler* compile *an* 8-bit *operation* that 
uses *bitwise* operators *into* a *16-bit* operation *in* 
*assembly?*

Bitwise operations in Standard C will automatically promote their 
operands to an int, which is (by default) 16 bits in avr-gcc.

To work around this use typecasts on the operands, including literals, 
to declare that the values are to be 8 bit operands.

This may be especially important when clearing a bit:
1
var &= ~mask;  /* wrong way! */

The bitwise "not" operator (~) will also promote the value in mask to an 
int. To keep it an 8-bit value, typecast before the "not" operator:
1
var &= (unsigned char)~mask;"

von Klaus (Gast)


Lesenswert?

> Why does the compiler compile an 8-bit operation that
> uses bitwise operators into a 16-bit operation *in*
> *assembly?*

War dir langweilig, oder bist du einfach nur geistig verwirrt? ;)


Zum Thema: ich konnte so ein Verhalten GCC bisher nicht beobachten. Ich 
benutze keine solchen casts, und der gcc hat mir das immer brav in 8 Bit 
Operationen ausgeführt. Welche Version ich benutzt hab, weiß ich gerad 
nicht, aber es war auf jedenfall eine halbwegs aktuelle.

von Gast (Gast)


Lesenswert?

5 Sekunden: " " -> "* *"

Danke für die Antwort.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.