Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10269

Re: datatype for bit flags

From jacob navia <jacob@spamsink.net>
Newsgroups comp.lang.c, comp.lang.c++
Subject Re: datatype for bit flags
Date 2011-09-15 09:26 +0200
Organization lccwin
Message-ID <j4s9c0$scv$1@speranza.aioe.org> (permalink)
References <j4s877$2a0$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


Le 15/09/11 09:07, Gr a écrit :
> I am not an expert C or C++ programmer but know enough to get by.
>
> Currently studying some graphics algorithms as a part of CAD.
>
> In the Cohen Sutherland Algorithm of line clipping, a code/flag is using
> for denoting where an endpoint of the line lies as compared to the
> clipping rectangle.
>
> This code consists of 4 flags (1 each for left, right, below & above).
>
> type code = 0;
>
> if(point lies to the left of clipping window)
> code = code & 1;
> if(point lies to right)
> code = code & 2;
>
> if(point lies below)
> code = code & 4;
> if(point lies above)
> code = code & 8;
>
>
> Now in some books the type of code is declared
> as
> unsigned char code;
> In others, it's
> int code;
>
> which is better?
>

unsigned code;

would be better since you are doing bit operations with
the data.

> unsigned char code saves space

It depends of the alignment requirements of the rest of the
data. If you want to make an array of 20 000 of those flags
then unsigned char would be better. For just a single flag
int would be better since the machine doesn't have to
clear a register and then load the character into it when
operating on it.


> (is the unsigned really necessary anyway?)
>

It depends on the operations you are doing with the data.
By the way are you sure you want
	code = code & 1
This sets to zero all bits but the last... If the last was zero
it remains zero.

Or maybe you intended
	code = code | 1;

That *sets* the last bit to 1.

> but I think int is probably faster.
>
No

> Can someone explain what's a better way to do this if you were
> writing a graphics library?

You would have to explain more what you want to do. Now, you are doing
nothing, i.e.  if you have an int with all zeros, after your operations
the data remains the same: all zeroes.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

datatype for bit flags Gr <g@g.com> - 2011-09-15 12:37 +0530
  Re: datatype for bit flags jacob navia <jacob@spamsink.net> - 2011-09-15 09:26 +0200
    Re: datatype for bit flags Gr <g@g.com> - 2011-09-15 13:33 +0530
      Re: datatype for bit flags jacob navia <jacob@spamsink.net> - 2011-09-15 10:12 +0200
  Re: datatype for bit flags Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-15 01:06 -0700
  Re: datatype for bit flags "BartC" <bc@freeuk.com> - 2011-09-15 10:56 +0100
    Re: datatype for bit flags Gr <g@g.com> - 2011-09-15 17:24 +0530
  Re: datatype for bit flags Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-16 01:16 +0300
    Re: datatype for bit flags Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-16 04:59 -0700
      Re: datatype for bit flags Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-16 20:46 +0000
        Re: datatype for bit flags pete <pfiland@mindspring.com> - 2011-09-16 18:00 -0400
        Re: datatype for bit flags Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-09-18 05:00 -0700
          Re: datatype for bit flags Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-18 13:36 +0000
      Re: datatype for bit flags "Tony" <nospamme@ever.net> - 2011-09-16 16:47 -0500
        Re: datatype for bit flags pete <pfiland@mindspring.com> - 2011-09-16 18:35 -0400
          Re: datatype for bit flags Keith Thompson <kst-u@mib.org> - 2011-09-16 16:25 -0700
            Re: datatype for bit flags pete <pfiland@mindspring.com> - 2011-09-17 02:35 -0400
              Re: datatype for bit flags Keith Thompson <kst-u@mib.org> - 2011-09-17 14:42 -0700
          Re: datatype for bit flags "Tony" <nospamme@ever.net> - 2011-09-16 19:27 -0500
  Re: datatype for bit flags hanukas <jukka@liimatta.org> - 2011-09-23 03:56 -0700

csiph-web