[cc65-devel] Fwd: Re: bitfields again
cc65 - a freeware C compiler for 6502 based systems
Brought to you by:
gpz
|
From: Vorsichtphysiker <vor...@gm...> - 2017-12-10 15:20:21
|
-------- Forwarded Message --------
Subject: Re: [cc65-devel] bitfields again
Date: Sat, 9 Dec 2017 23:18:31 +0100
From: Vorsichtphysiker <vor...@gm...>
To: Greg King <gre...@ve...>
No, sorry.
Here is my updated program. This has been compiled without -O switch, to
be sure.
It outputs 0 1 0 1 0 1 0 instead of 0 1 0 2 0 4 0, like gcc does.
#include <stdio.h>
#include <stdlib.h>
typedef union{
unsigned int bf;
struct{
unsigned int a:1;
unsigned int b:1;
unsigned int c:1;
};
} bitfield_t;
int main()
{
bitfield_t bitfield = {0};
printf("Bitfield: %u\n", bitfield.bf);
bitfield.a = bitfield.a ^ 1;
printf("a=1: %u\n", bitfield.bf);
bitfield.a = bitfield.a ^1;
printf("a=0: %u\n\n", bitfield.bf);
bitfield.b = bitfield.b ^1;
printf("b=1: %u\n", bitfield.bf);
bitfield.b = bitfield.b ^ 1;
printf("b=0: %u\n\n", bitfield.bf);
bitfield.c = bitfield.c ^1;
printf("c=1: %u\n", bitfield.bf);
bitfield.c = bitfield.c ^ 1;
printf("c=0: %u\n\n", bitfield.bf);
return 0;
}
On 07.12.2017 21:42, Greg King wrote:
> On 2017-12-05 10:31 AM, Stefan wrote:
>> Sorry to tell, but the workaround using x = 1-x does not work, too.
>> All what is left as a workaround is using conditionals to toggle
>> bitfield;
>> or, manually read the value, store it in a variable, toggle that
>> variable, and
>> then set it again. I don't think that would be an acceptable
>> alternative.
>
> Does "expanding" the ^= operator work in your program?
>
> myBF.asBF.f3 = myBF.asBF.f3 ^ 1;
>
> ------------------------------------------------------------------------------
>
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________________
> cc6...@li...
> https://lists.sourceforge.net/lists/listinfo/cc65-devel
|