Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Bit field usage Date: Thu, 24 Oct 2019 17:50:51 -0700 Organization: A noiseless patient Spider Lines: 25 Message-ID: <86woctfy1g.fsf@linuxsc.com> References: <1bo8y6azlw.fsf@pfeifferfamily.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="294289d8fb0afb0e43f75d31b2b6cc13"; logging-data="22943"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DRgRgEx6sxDrEs5oUIDw23XXIy73BcF4=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:dEm9yQWdeSBlIPnLKl8gJ6T7ehk= sha1:anIpLWkIFMd3hiPIm9zxRqt4vtU= Xref: csiph.com comp.lang.c:147220 Joe Pfeiffer writes: > jacobnavia writes: > >> 1) To access a bitfield, the compiler generates a sequence of shifts >> that increases execution time. >> >> 2) The instructions sequence of the shifts increases code size. >> >> Obviously, if you have more than 64 flags (in a 64 bit machine) or >> more than 32 flags in a 32 bit machine, bitfields can save a lot of >> data space: you can fit up to 128 bitfields in 16 bytes, instead of >> 128 bytes if you use a full char for each bit field. >> >> It is important to check two things: >> 1) If a bit field is heavily used, consider transforming it into an >> unsigned char. >> 2) If you have a few bit fields with bitsize less than the word size >> of your machine, consider if the alignment requirements are nullifying >> any space gains obtained by using bit fields. > > I got burned badly enough by bitfields doing unexpected things decades > ago that I abandoned them and do my own shifts and masks. Out of curiosity, what kind of problems did you run into?