character array as bits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prakash437

    character array as bits

    hello everyone,
    I have asked a similar question on the C++ group, however, i did not
    get the answer i was expecting ...

    How can i use a unsigned char array (of size, say 10 characters) as a
    set of 80 bits, and apply a whole range of bit operations (>>, <<, &
    |) etc ...

    Thanks for your time ..
    Prakash
  • Ali Karaali

    #2
    Re: character array as bits

    On 17 Aralýk, 23:42, prakash437 <prakash...@gma il.comwrote:
    hello everyone,
    I have asked a similar question on the C++ group, however, i did not
    get the answer i was expecting ...
    >
    How can i use a unsigned char array (of size, say 10 characters) as a
    set of 80 bits, and apply a whole range of bit operations (>>, <<, &
    |) etc ...
    >
    Thanks for your time ..
    Prakash
    #include <stdio.h>

    int main()
    {
    unsigned char array[10];
    int i, j, bit;

    for( i = 0; i < 10; ++i) {
    for( j = 0; j < 8; ++j) {
    bit = (array[i] >j) & 1;
    printf("%d", bit);
    }
    printf(" ");
    }

    return 0;
    }

    Comment

    • Walter Roberson

      #3
      Re: character array as bits

      In article <dfdd9cca-1424-4ac7-bb64-c3d8bfc8595d@i3 g2000hsf.google groups.com>,
      prakash437 <prakash437@gma il.comwrote:
      >I have asked a similar question on the C++ group, however, i did not
      >get the answer i was expecting ...
      >How can i use a unsigned char array (of size, say 10 characters) as a
      >set of 80 bits, and apply a whole range of bit operations (>>, <<, &
      >|) etc ...
      You cannot do that in standard C, except by writing routines that
      do the operations part-by-part.
      --
      So you found your solution
      What will be your last contribution?
      -- Supertramp (Fool's Overture)

      Comment

      • user923005

        #4
        Re: character array as bits

        On Dec 17, 1:42 pm, prakash437 <prakash...@gma il.comwrote:
        hello everyone,
        I have asked a similar question on the C++ group, however, i did not
        get the answer i was expecting ...
        >
        How can i use a unsigned char array (of size, say 10 characters) as a
        set of 80 bits, and apply a whole range of bit operations (>>, <<, &
        |) etc ...
        It's a FAQ:
        20.8: How can I implement sets or arrays of bits?

        A: Use arrays of char or int, with a few macros to access the
        desired bit at the proper index. Here are some simple macros
        to
        use with arrays of char:

        #include <limits.h /* for CHAR_BIT */

        #define BITMASK(b) (1 << ((b) % CHAR_BIT))
        #define BITSLOT(b) ((b) / CHAR_BIT)
        #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
        #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

        (If you don't have <limits.h>, try using 8 for CHAR_BIT.)

        References: H&S Sec. 7.6.7 pp. 211-216.

        Comment

        • pete

          #5
          Re: character array as bits

          prakash437 wrote:
          >
          hello everyone,
          I have asked a similar question on the C++ group, however, i did not
          get the answer i was expecting ...
          >
          How can i use a unsigned char array (of size, say 10 characters) as a
          set of 80 bits, and apply a whole range of bit operations (>>, <<, &
          |) etc ...
          The way that you can:
          use a unsigned char array (of size, say 10 characters)
          as a set of 80 bits,
          and apply a whole range of bit operations (>>, <<, &, |) etc ...
          is:
          one byte at a time.

          --
          pete

          Comment

          • Martin Ambuhl

            #6
            Re: character array as bits

            prakash437 wrote:
            hello everyone,
            I have asked a similar question on the C++ group, however, i did not
            get the answer i was expecting ...
            >
            How can i use a unsigned char array (of size, say 10 characters) as a
            set of 80 bits, and apply a whole range of bit operations (>>, <<, &
            |) etc ...
            Those bit operations are defined only for unsigned integer scalars. You
            could, of course, do something like

            #include <string.h>

            int main(void)
            {
            unsigned long shiftreg; /* long long doesn't exist in
            C++ */
            unsigned char chararray[sizeof(unsigned long)];

            /* code that does other stuff, including storing stuff in char array */

            memcpy(&shiftre g, chararray, sizeof shiftreg);
            /* do bit operations on the chars stored in shiftreg */
            memcpy(chararra y, &shiftreg, sizeof shiftreg);

            /* etc */
            return 0;
            }

            Comment

            • prakashraovaddina@googlemail.com

              #7
              Re: character array as bits

              Thanks for your replies ..

              However, what i am looking is not something to mask or unmask a bit
              but something that could allow me to treat the whole buffer as a set
              of bits ..

              Importantly i am looking to apply a shift operation whose effect can
              be felt on the whole buffer .. However, i assume such an
              implementation would be quite difficult unless i am ready to work on
              each byte at a time ..

              Prakash

              On Dec 18, 12:22 am, pete <pfil...@mindsp ring.comwrote:
              prakash437 wrote:
              >
              hello everyone,
              I have asked a similar question on the C++ group, however, i did not
              get the answer i was expecting ...
              >
              How can i use a unsigned char array (of size, say 10 characters) as a
              set of 80 bits, and apply a whole range of bit operations (>>, <<, &
              |) etc ...
              >
              The way that you can:
              use a unsigned char array (of size, say 10 characters)
              as a set of 80 bits,
              and apply a whole range of bit operations (>>, <<, &, |) etc ...
              is:
              one byte at a time.
              >
              --
              pete

              Comment

              • Peter Nilsson

                #8
                Re: character array as bits

                [Please don't top post. Corrected.]

                prakashraovadd. ..@googlemail.c om wrote:
                pete <pfil...@mindsp ring.comwrote:
                The way that you can:
                use a unsigned char array (of size, say 10 characters)
                as a set of 80 bits,
                and apply a whole range of bit operations (>>, <<, &, |)
                etc ...
                is:
                one byte at a time.
                >
                ...i am looking to apply a shift operation whose effect can
                be felt on the whole buffer .. However, i assume such an
                implementation would be quite difficult unless i am ready to
                work on each byte at a time ..
                No, such an implementation is quite _easy_ if you are ready
                to work on each byte at a time.

                Note: You can also use unsigned int (or unsigned long), not
                just unsigned char, to implement bit sets.

                --
                Peter

                Comment

                • Keith Thompson

                  #9
                  Re: character array as bits

                  user923005 <dcorbit@connx. comwrites:
                  On Dec 17, 1:42 pm, prakash437 <prakash...@gma il.comwrote:
                  >I have asked a similar question on the C++ group, however, i did not
                  >get the answer i was expecting ...
                  >>
                  >How can i use a unsigned char array (of size, say 10 characters) as a
                  >set of 80 bits, and apply a whole range of bit operations (>>, <<, &
                  >|) etc ...
                  >
                  It's a FAQ:
                  20.8: How can I implement sets or arrays of bits?
                  >
                  A: Use arrays of char or int, with a few macros to access the
                  desired bit at the proper index. Here are some simple macros to
                  use with arrays of char:
                  >
                  #include <limits.h /* for CHAR_BIT */
                  [snip]
                  (If you don't have <limits.h>, try using 8 for CHAR_BIT.)
                  >
                  References: H&S Sec. 7.6.7 pp. 211-216.
                  Hmm. I'm a little surprised the FAQ still has that "If you don't have
                  <limits.h>" clause. There should be vanishingly few C implementations
                  still in use that don't have it. (Yes, it's required for freestanding
                  implementations as well.)

                  --
                  Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                  Looking for software development work in the San Diego area.
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • user923005

                    #10
                    Re: character array as bits

                    On Dec 17, 10:01 pm, Keith Thompson <ks...@mib.orgw rote:
                    user923005 <dcor...@connx. comwrites:
                    On Dec 17, 1:42 pm, prakash437 <prakash...@gma il.comwrote:
                    I have asked a similar question on the C++ group, however, i did not
                    get the answer i was expecting ...
                    >
                    How can i use a unsigned char array (of size, say 10 characters) as a
                    set of 80 bits, and apply a whole range of bit operations (>>, <<, &
                    |) etc ...
                    >
                    It's a FAQ:
                    20.8: How can I implement sets or arrays of bits?
                    >
                    A: Use arrays of char or int, with a few macros to access the
                    desired bit at the proper index. Here are some simple macros to
                    use with arrays of char:
                    >
                    #include <limits.h /* for CHAR_BIT */
                    [snip]
                    (If you don't have <limits.h>, try using 8 for CHAR_BIT.)
                    >
                    References: H&S Sec. 7.6.7 pp. 211-216.
                    >
                    Hmm. I'm a little surprised the FAQ still has that "If you don't have
                    <limits.h>" clause. There should be vanishingly few C implementations
                    still in use that don't have it. (Yes, it's required for freestanding
                    implementations as well.)
                    I have some older Unix-type machines here that have <values.hinstea d
                    of limits.h. People tend to keep Unix machines a long time (longer
                    than they should really) and so we support ancient 1980's stuff. I
                    would say it is still pertinent because real systems that people are
                    using to get work done are still configured in that way. I did
                    recently show one customer that a used, 5 year old machine {same
                    vendor chain, but updated by a decade from what they were using} would
                    outperform their relic by at least 20:1 up to 500:1 in all areas
                    (disk, CPU, Net, etc.).

                    Comment

                    • Keith Thompson

                      #11
                      Re: character array as bits

                      Martin Ambuhl <mambuhl@earthl ink.netwrites:
                      prakash437 wrote:
                      >hello everyone,
                      >I have asked a similar question on the C++ group, however, i did not
                      >get the answer i was expecting ...
                      >>
                      >How can i use a unsigned char array (of size, say 10 characters) as a
                      >set of 80 bits, and apply a whole range of bit operations (>>, <<, &
                      >|) etc ...
                      >
                      Those bit operations are defined only for unsigned integer scalars.
                      [snip]

                      Well, that's not *quite* correct. The bitwise operations are defined
                      for both signed and unsigned types. But for signed types,
                      particularly for negative values, the results can be
                      implementation-defined and/or undefined (I haven't bothered to
                      memorize which it is).

                      Bitwise operations on signed types are rarely as useful as bitwise
                      operations on unsigned types, but the compiler probably won't warn you
                      if you apply them to signed types.

                      --
                      Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                      Looking for software development work in the San Diego area.
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • pete

                        #12
                        Re: character array as bits

                        prakashraovaddi na@googlemail.c om wrote:
                        >
                        Thanks for your replies ..
                        >
                        However, what i am looking is not something to mask or unmask a bit
                        but something that could allow me to treat the whole buffer as a set
                        of bits ..
                        >
                        Importantly i am looking to apply a shift operation whose effect can
                        be felt on the whole buffer .. However, i assume such an
                        implementation would be quite difficult unless i am ready to work on
                        each byte at a time ..
                        The biggest thing that shift operators will work on,
                        is the biggest integer type.

                        If you alter your goal,
                        you could use macros to work with sizeof(int) bytes at a time,
                        on objects
                        that don't have size or alignment problems with type unsigned,
                        accessing them through an (unsigned int) type lvalue,
                        (or sizeof(long) or maybe long long, if you write your own macros)
                        /*
                        ** Some bitwise macros for unsigned U
                        */
                        #define READ_UBIT(U, N) ((U) > (N) & 1u)
                        #define FLIP_UBIT(U, N) ((void)((U) ^= 1u << (N)))
                        #define SET_UBIT(U, N) ((void)((U) |= 1u << (N)))
                        #define CLEAR_UBIT(U, N) ((void)((U) &= ~(1u << (N))))

                        --
                        pete

                        Comment

                        • Ben Pfaff

                          #13
                          Re: character array as bits

                          pete <pfiland@mindsp ring.comwrites:
                          #define READ_UBIT(U, N) ((U) > (N) & 1u)
                          Perhaps you have memorized the relative precedence of >and &.
                          But many programmers have not. Thus, I would prefer to see this
                          written as:
                          #define READ_UBIT(U, N) (((U) > (N)) & 1u)
                          --
                          char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
                          ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
                          =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
                          2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

                          Comment

                          • pete

                            #14
                            Re: character array as bits

                            Ben Pfaff wrote:
                            >
                            pete <pfiland@mindsp ring.comwrites:
                            >
                            #define READ_UBIT(U, N) ((U) > (N) & 1u)
                            >
                            Perhaps you have memorized the relative precedence of >and &.
                            But many programmers have not. Thus, I would prefer to see this
                            written as:
                            #define READ_UBIT(U, N) (((U) > (N)) & 1u)
                            OK, next time.

                            --
                            pete

                            Comment

                            • CBFalconer

                              #15
                              Re: character array as bits

                              prakashraovaddi na@googlemail.c om wrote:
                              >
                              However, what i am looking is not something to mask or unmask a
                              bit but something that could allow me to treat the whole buffer
                              as a set of bits ..
                              Please do not top-post. Your answer belongs after (or intermixed
                              with) the quoted material to which you reply, after snipping all
                              irrelevant material. See the following links:

                              --
                              <http://www.catb.org/~esr/faqs/smart-questions.html>
                              <http://www.caliburn.nl/topposting.html >
                              <http://www.netmeister. org/news/learn2quote.htm l>
                              <http://cfaj.freeshell. org/google/ (taming google)
                              <http://members.fortune city.com/nnqweb/ (newusers)



                              --
                              Posted via a free Usenet account from http://www.teranews.com

                              Comment

                              Working...