bitwise operators

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

    #1

    bitwise operators

    hi,

    I need to write a function that will check whether x is nonzero. Return 0
    if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
    << >> and I am not allowed to use an loops.

    My program so far looks like:

    int isNonZero(int x) {


    x = (1 << x) & x;
    x = ~x;
    x = x & 1;
    return x;


    }

    All cases work except for the below.


    Test isNonZero(-2147483648[0x80000000]) failed.
    Gives 0[0x0]. Should be 1[0x1]

    If tried everything I could think of and I am all out of ideas. Any one
    have any idea how I could fix my code to deal with this negative number?


    I also need to write one that will check whether x is nonzero using only ~
    & ^ | + << >>

    My code so far is:
    int isLess(int x, int y) {

    int a =0;

    a = x ^ y;
    a = y >> a;
    a = a + 1;
    a = !a;
    return !a;
    }

    Which only works for about half the cases (on small non-negative numbers)


    Any input would be appreciated.
    Thanks,

  • infobahn

    #2
    Re: bitwise operators

    77scrapper77 wrote:[color=blue]
    >
    > I need to write a function that will check whether x is nonzero. Return 0
    > if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
    > << >> and I am not allowed to use an loops.
    >
    > My program so far looks like:
    >
    > int isNonZero(int x) {
    >
    > x = (1 << x) & x;[/color]

    Undefined behaviour for most values of x. The Standard has this to
    say about bit-shifting:

    "If the value of the right operand is negative or is greater than
    or equal to the width in bits of the promoted left operand, the
    behavior is undefined."

    Also, = is an operator, and it's not in your list of allowed operators.

    Personally, I don't think it's possible in correct C with just the
    operators that you are allowed to use (although I am ready to be
    proved wrong).

    Comment

    • Michael Mair

      #3
      Re: bitwise operators

      77scrapper77 wrote:[color=blue]
      > hi,
      >
      > I need to write a function that will check whether x is nonzero. Return 0
      > if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
      > << >> and I am not allowed to use an loops.
      >
      > My program so far looks like:
      >
      > int isNonZero(int x) {[/color]
      This function name invades the implementation' s namespace
      as it starts with "is". Make that IsNonZero().[color=blue]
      >
      >
      > x = (1 << x) & x;[/color]

      Undefined behaviour, see infobahn's message.
      [color=blue]
      > x = ~x;
      > x = x & 1;
      > return x;
      >
      >
      > }
      >
      > All cases work except for the below.[/color]

      Nope: x=0 ... (~((1<<0) & 0)) & 1 == 1.
      So your most important case does not work.

      I do not see at all how you can do anything sensible for signed
      integers as they could have negative zeros for 1s complement and
      sign-magnitude representations . Seems to be a completely hopeless
      task. Even for 2s complement only, I do not have the least clue
      how to do it.
      [color=blue]
      > Test isNonZero(-2147483648[0x80000000]) failed.
      > Gives 0[0x0]. Should be 1[0x1]
      >
      > If tried everything I could think of and I am all out of ideas. Any one
      > have any idea how I could fix my code to deal with this negative number?[/color]

      No. Not at all.

      [color=blue]
      > I also need to write one that will check whether x is nonzero using only ~[/color]

      ITYM whether x<y
      [color=blue]
      > & ^ | + << >>
      >
      > My code so far is:
      > int isLess(int x, int y) {[/color]

      Again: The function name.
      [color=blue]
      > int a =0;
      >
      > a = x ^ y;
      > a = y >> a;[/color]

      You seem to be completely clueless what the shift operators are doing.
      [color=blue]
      > a = a + 1;
      > a = !a;
      > return !a;[/color]

      ! is an operator, too, and not on your list.
      If it was not, your IsNonZero() function's body would consist of
      {return !!x;}
      [color=blue]
      > }
      >
      > Which only works for about half the cases (on small non-negative numbers)
      >
      > Any input would be appreciated.[/color]

      Are you allowed to use if, else, ...?
      Either you are not telling us the whole truth or you should look
      for someone different to give you C problems.


      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • Ben Pfaff

        #4
        Re: bitwise operators

        "77scrapper 77" <gregarious_gir l77@nospam.hotm ail.com> writes:
        [color=blue]
        > I need to write a function that will check whether x is nonzero. Return 0
        > if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
        > << >> and I am not allowed to use an loops.[/color]

        int is_nonzero (int x)
        {
        switch (x) {
        case 0:
        return 0;
        default:
        return 1;
        }
        }
        --
        "Some programming practices beg for errors;
        this one is like calling an 800 number
        and having errors delivered to your door."
        --Steve McConnell

        Comment

        • Old Wolf

          #5
          Re: bitwise operators

          77scrapper77 wrote:[color=blue]
          > hi,
          >
          > I need to write a function that will check whether x is nonzero.[/color]
          Return 0[color=blue]
          > if x is = to 0 and 1 otherwise. The open operators I can use are ~ &[/color]
          ^ | +[color=blue]
          > << >> and I am not allowed to use an loops.
          >[/color]

          int isNonZero(int x)
          {
          if (x)
          return 1;
          return 0;
          }

          Comment

          • 77scrapper77

            #6
            Re: bitwise operators

            I can't use any loops or if statements. I can use = though.



            Comment

            • Martin Johansen

              #7
              Re: bitwise operators

              int isNonZero(int x) {
              return(x || 0);
              }


              Comment

              • 77scrapper77

                #8
                Re: bitwise operators

                I can't use || either, just |.



                Comment

                • Chris Croughton

                  #9
                  Re: bitwise operators

                  On Sun, 20 Feb 2005 19:55:08 +0100, Michael Mair
                  <Michael.Mair@i nvalid.invalid> wrote:
                  [color=blue]
                  > 77scrapper77 wrote:[color=green]
                  >> hi,
                  >>
                  >> I need to write a function that will check whether x is nonzero. Return 0
                  >> if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
                  >> << >> and I am not allowed to use an loops.
                  >>
                  >> My program so far looks like:
                  >>
                  >> int isNonZero(int x) {[/color]
                  > This function name invades the implementation' s namespace
                  > as it starts with "is". Make that IsNonZero().[/color]

                  Not true. From section 7.26 "Future Library Directions":

                  7.26.2 Character handling <ctype.h>

                  1 Function names that begin with either is or to, and a lowercase
                  letter may be added to the declarations in the <ctype.h> header.

                  It is allowed to start an identifier with is and an uppercase letter,
                  which is what he did. The same is true as far as I can see with most of
                  the other namespace restrictions (function names starting with str, mem
                  or wcs ans a lowercase letter are banned, ones with an uppercase letter
                  are permitted).
                  [color=blue]
                  > Are you allowed to use if, else, ...?
                  > Either you are not telling us the whole truth or you should look
                  > for someone different to give you C problems.[/color]

                  It looks like homework to me...

                  Chris C

                  Comment

                  • Ben Pfaff

                    #10
                    Re: bitwise operators

                    Chris Croughton <chris@keristor .net> writes:
                    [color=blue]
                    > <Michael.Mair@i nvalid.invalid> wrote:[color=green]
                    >> 77scrapper77 wrote:[color=darkred]
                    >>> int isNonZero(int x) {[/color]
                    >> This function name invades the implementation' s namespace
                    >> as it starts with "is". Make that IsNonZero().[/color]
                    >
                    > Not true. From section 7.26 "Future Library Directions":
                    >
                    > 7.26.2 Character handling <ctype.h>
                    >
                    > 1 Function names that begin with either is or to, and a lowercase
                    > letter may be added to the declarations in the <ctype.h> header.[/color]

                    However, C89 implementations are allowed to have a
                    case-insensitive linker.
                    --
                    int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                    \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                    );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                    );}return 0;}

                    Comment

                    • Clark S. Cox III

                      #11
                      Re: bitwise operators

                      On 2005-02-20 12:45:49 -0500, "77scrapper 77"
                      <gregarious_gir l77@nospam.hotm ail.com> said:
                      [color=blue]
                      > hi,
                      >
                      > I need to write a function that will check whether x is nonzero. Return 0
                      > if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
                      > << >> and I am not allowed to use an loops.[/color]

                      In C99:

                      int isNonZero(int x)
                      {
                      _Bool b = x;
                      return b;
                      }

                      --
                      Clark S. Cox, III
                      clarkcox3@gmail .com

                      Comment

                      • Spacen Jasset

                        #12
                        Re: bitwise operators

                        Michael Mair wrote:[color=blue]
                        > ! is an operator, too, and not on your list.
                        > If it was not, your IsNonZero() function's body would consist of
                        > {return !!x;}
                        >[color=green]
                        >> }[/color][/color]

                        This has it. That's the first thing I thought of. Any excuse to stack up
                        operators.

                        If you don't like that then, try return !!!!x; instead


                        It's best not to use tricks though, readability is paramount.

                        also, this will suffice, return x ? 1 : 0 ;

                        Comment

                        • 77scrapper77

                          #13
                          Re: bitwise operators

                          I can't use the ! operator.

                          Comment

                          • roman ziak

                            #14
                            Re: bitwise operators

                            77scrapper77 wrote:[color=blue]
                            > hi,
                            >
                            > I need to write a function that will check whether x is nonzero. Return 0
                            > if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
                            > << >> and I am not allowed to use an loops.
                            >
                            > My program so far looks like:
                            >
                            > int isNonZero(int x) {
                            >
                            >
                            > x = (1 << x) & x;
                            > x = ~x;
                            > x = x & 1;
                            > return x;
                            >
                            >
                            > }[/color]

                            I am not sure if standard defines the case when you are shifting left by
                            more than the bit size of integer.

                            But how about:

                            N-1
                            ---
                            [ \ (x >> i) ] & 1
                            /
                            ---
                            i=0

                            where N is the bit size of word, so for 4 bit machine the expression is:

                            ( x + (x>>1) + (x>>2) + (x>>4) ) & 1

                            Comment

                            • roman ziak

                              #15
                              Re: bitwise operators

                              > N-1[color=blue]
                              > ---
                              > [ \ (x >> i) ] & 1
                              > /
                              > ---
                              > i=0
                              >
                              > where N is the bit size of word, so for 4 bit machine the expression is:
                              >
                              > ( x + (x>>1) + (x>>2) + (x>>4) ) & 1[/color]

                              correction:

                              ( x + (x>>1) + (x>>2) + (x>>3) ) & 1

                              Comment

                              Working...