Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: (Bit offtopic) ANSI C, bitfields and anonymous unions

Author: Robert Hyatt

Date: 18:25:05 06/26/00

Go up one level in this thread


On June 26, 2000 at 17:10:40, Gian-Carlo Pascutto wrote:

>Hi all,
>
>I'm wondering whether the following code is legal ANSI C:
>
>typedef struct {
>  int from;
>  int target;
>  int captured;
>  int cap_num;
>  union
>  {
>      unsigned int castled:4, was_promoted:1, ep:1, promoted:4;
>      unsigned int xx;
>  };
>} move_s;
>
>The idea is to do
>
>xx = 0;
>
>when I am sure that it is a normal move, rather than to have to set
>each field individually.
>
>MSVC++ has no trouble with this code, but the GNU C Compiler barfs
>on it.
>
>I suppose one way to fix this would be to name the union, but that
>would require all code which uses the castled, was_promoted, ...
>fields to be rewritten...not a nice prospect.
>
>--
>GCP





That ought to work ok, but it has some severe portability problems.  The
ANSI standard doesn't specify from which "end" of the word the bit fields
start.  Nor does it specify whether the default is "signed" or "unsigned".

I used them very early in Crafty, but went to a straight int and do my own
anding/oring/shifting to extract things.

The "union" is probably asking for grief due to the non-specified start bit
for bit fields.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.