8 bit Bitmanipulation casten, sonst 16 bit?

Gast #1511610
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;"
Gast #1511644
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.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren