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


Groups > comp.lang.c > #163280

Re: Check if all 256 bits are clear or set ?

Path csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date Wed, 03 Nov 2021 04:08:18 -0500
Message-Id <sm095i-892.ln1@aretha.foo>
From Peter 'Shaggy' Haywood <phaywood@alphalink.com.au>
Subject Re: Check if all 256 bits are clear or set ?
Newsgroups comp.lang.c
Date Wed, 03 Nov 2021 11:11:39 +1100
References <e829b7da-73c3-434a-9284-ab3b90f4e566n@googlegroups.com> <bits-20211029021300@ram.dialup.fu-berlin.de>
User-Agent KNode/0.10.9
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7Bit
Lines 80
X-Usenet-Provider http://www.giganews.com
X-Trace sv3-8g1UJnJuHOeiRg5ERwi5IuKJ08DQs+zuCPQ9m98GLO4KSGaQa7aExd/Qy3zyCr3OdklolbzXlK0Vz4W!FnPrhXnPgJONVHqxKU82PUc/bYWdQx6HgUHxW3GhW8Q9tbF2LWU=
X-Complaints-To abuse@giganews.com
X-DMCA-Notifications http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info Otherwise we will be unable to process your complaint properly
X-Postfilter 1.3.40
X-Original-Bytes 2827
Xref csiph.com comp.lang.c:163280

Show key headers only | View raw


Groovy hepcat Stefan Ram was jivin' in comp.lang.c on Fri, 29 Oct 2021
12:19 pm. It's a cool scene! Dig it.

> skybuck2000 <skybuck2000@hotmail.com> writes:
>>The code needs to check if all bits are empty or if all bits
>>are full. So basically all clear or all set.
> 
>   I'd set up two constant arrays, kinda like
> 
> uint32_t const f = 0xffffffff;
> uint32_t const a0[ 8 ]={ 0, 0, 0, 0, 0, 0, 0, 0, };
> uint32_t const a1[ 8 ]={ f, f, f, f, f, f, f, f, };
> 
>   , and then use memcmp with those arrays:
> 
> if( !memcmp( ( void * )&x, ( void * )&a0, sizeof( x )))
> puts( "x is all zero" );
> 
>   . And only do more work /if/ I then learned that
>   this was too slow.

  Here's a more flexible (but untested) way:

#define ALL_CLEAR 1
#define ALL_SET 2

unsigned all_bits_clear_or_set(unsigned char *data, size_t n)
{
  unsigned clrset = 3;
  size_t i;

  for(i = 0; i < n; i++)
  {
    if((unsigned char)~0U == data[i])
    {
      clrset ^= ALL_SET;
      break;
    }
  }

  for(i = 0; i < n; i++)
  {
    if(0 == data[i])
    {
      clrset ^= ALL_CLEAR;
      break;
    }
  }

  return clrset;
}

sometype calling_function(some paramlist)
{
  unsigned clrset;

  /* some code */
  clrset = all_bits_clear_or_set(somedata, size_of_somedata_in_bytes);
  if(clrset & ALL_CLEAR)
  {
    /* All bits are clear. */
  }
  else if(clrset & ALL_SET)
  {
    /* All bits are set. */
  }
  /* some more code */
}

  That's a more general solution, allowing for data of other sizes to be
tested. I like to write flexible, reuseable code. :)

-- 


----- Dig the NEW and IMPROVED news sig!! -----


-------------- Shaggy was here! ---------------
              Ain't I'm a dawg!!

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


Thread

Check if all 256 bits are clear or set ? skybuck2000 <skybuck2000@hotmail.com> - 2021-10-28 17:47 -0700
  Re: Check if all 256 bits are clear or set ? skybuck2000 <skybuck2000@hotmail.com> - 2021-10-28 18:02 -0700
    Re: Check if all 256 bits are clear or set ? skybuck2000 <skybuck2000@hotmail.com> - 2021-10-28 18:12 -0700
      Re: Check if all 256 bits are clear or set ? skybuck2000 <skybuck2000@hotmail.com> - 2021-10-28 19:18 -0700
        Re: Check if all 256 bits are clear or set ? skybuck2000 <skybuck2000@hotmail.com> - 2021-10-29 05:16 -0700
  Re: Check if all 256 bits are clear or set ? Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2021-11-03 11:11 +1100
    Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-03 11:50 +0000
      Re: Check if all 256 bits are clear or set ? Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2021-11-06 16:08 +1100
        Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 11:02 +0000
          Re: Check if all 256 bits are clear or set ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-06 15:04 -0700
            Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-06 22:06 +0000
            Re: Check if all 256 bits are clear or set ? Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2021-11-13 11:54 +1100
          Re: Check if all 256 bits are clear or set ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-06 15:22 -0700
            Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-07 00:08 +0000
              Re: Check if all 256 bits are clear or set ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-07 08:12 -0800
        Re: Check if all 256 bits are clear or set ? skybuck2000 <skybuck2000@hotmail.com> - 2021-11-23 08:23 -0800
      Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-06 15:01 +0100
  Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-03 21:04 +0100
    Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-03 21:09 +0100
      Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-03 21:11 +0100
        Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-03 21:25 +0100
          Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-04 07:05 +0100
            Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-04 11:37 +0000
              Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-04 17:58 +0100
                Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-04 17:13 +0000
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-04 18:15 +0100
                Re: Check if all 256 bits are clear or set ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-11-04 17:27 +0000
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-04 18:29 +0100
                Re: Check if all 256 bits are clear or set ? scott@slp53.sl.home (Scott Lurndal) - 2021-11-04 17:28 +0000
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-05 11:56 +0100
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-05 18:20 +0100
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-05 18:32 +0100
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-04 18:13 +0100
                Re: Check if all 256 bits are clear or set ? Manfred <noname@add.invalid> - 2021-11-04 19:28 +0100
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-04 20:29 +0100
                Re: Check if all 256 bits are clear or set ? Manfred <noname@add.invalid> - 2021-11-05 02:06 +0100
                Re: Check if all 256 bits are clear or set ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-11-05 08:57 +0100
              Re: Check if all 256 bits are clear or set ? Bart <bc@freeuk.com> - 2021-11-04 18:01 +0000

csiph-web