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


Groups > comp.lang.c > #16393

Re: bitwise operators(info)

Message-ID <4F256AF6.2BCB@mindspring.com> (permalink)
Date 2012-01-29 10:51 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: bitwise operators(info)
References (4 earlier) <0.e41cbc1eedd06ab4387e.20120128113315GMT.874nvg3vus.fsf@bsb.me.uk> <jg1fpk$s1q$1@dont-email.me> <0.2835a3ac4e2fe672524d.20120128224118GMT.87r4yj30xd.fsf@bsb.me.uk> <5275606b-2eef-4e1d-9bd5-7ad2b454778e@x6g2000pbk.googlegroups.com> <0.39c22a4cda991bf5217b.20120129145931GMT.874nve367g.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:
> 
> Ceriousmall <divadsmall@gmail.com> writes:
> 
> I've fixed up line breaks.
> There's something odd about the look of your posts.
> 
> > (K&R Exercise 2-6)
> > Write a function setbits(x,p,n,y)
> > that returns x with the n bits that
> > begin at position p set to the rightmost n bits of y, leaving the
> > other bits unchanged.
> >
> > ** p set to the rightmost n bits of y ** This has me at a loss as to
> > [t]he role of P. P is passed to the function as one value then reset
> > to the n most of y
> 
> No, p is not set at all.
> Your quote "p set to the rightmost bits..." is
> out of context.  Its is "the n bits that begin at position p" that get
> set.
> 
> > my code thus far......
> > unsigned setbits(unsigned x, int p, int n, unsigned y)
> 
> Good.  Always do bit operations on unsigned quantities if you can.
> Personally, I'd also make n and p unsigned since it makes no sense to
> call setbits with either n or p < 0.

I agree.

> 
> > {
> >       unsigned mask = ~(~0 << n);

The above line likely contains indefined behavior.

(~0) is a negative value of type (int),
except in a ones complement system which supports negative zeros.
The left shift of a negative value of type (int) is undefined.

However, (~0u) is equal to (UINT_MAX).


> >
> >         ***************************************
> >       *  /* p = y & mask; */                *
> >         *  /* return x & (mask << (p+1-n)) */ *
> >         *       first attempt                 *
> >         ***************************************
> > return x & (mask << ((((p & 0) | mask) & y)+1-n));
> > }  second attempt and still trying
> >
> > Im not looking for the answer
> > as I'd really like to work this out for
> > myself but rather some pointers
> > on where I'm going wrong here or where
> > my interpretation has gone wrong.
> 
> Try something simpler.  Soon the exercise will itself look simple.
> First, write a function clearbits(x, p, n) that returns x with the n
> bits starting at position n cleared (zeroed).  If that's too tricky,
> image what you'd write if p was always zero.  How would that code have
> to change when p > 0?
> 
> Next write rightmost(y, n) 
> that returns the right-most n bits of y.  How
> can you use rightmost and clearbits to write setbits?



-- 
pete

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


Thread

bitwise operators(info) Ceriousmall <divadsmall@gmail.com> - 2012-01-27 13:56 -0800
  Re: bitwise operators(info) John Gordon <gordon@panix.com> - 2012-01-27 22:03 +0000
  Re: bitwise operators(info) "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-01-27 17:35 -0500
    Re: bitwise operators(info) Ceriousmall <divadsmall@gmail.com> - 2012-01-27 16:22 -0800
  Re: bitwise operators(info) Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-27 16:52 -0800
    Re: bitwise operators(info) Keith Thompson <kst-u@mib.org> - 2012-01-27 17:06 -0800
      Re: bitwise operators(info) Harald van Dijk <truedfx@gmail.com> - 2012-01-27 19:06 -0800
        Re: bitwise operators(info) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-28 11:33 +0000
          Re: bitwise operators(info) James Kuyper <jameskuyper@verizon.net> - 2012-01-28 13:46 -0500
            Re: bitwise operators(info) Kaz Kylheku <kaz@kylheku.com> - 2012-01-28 19:01 +0000
            Re: bitwise operators(info) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-28 22:41 +0000
              Re: bitwise operators(info) Ceriousmall <divadsmall@gmail.com> - 2012-01-28 21:19 -0800
                Re: bitwise operators(info) Keith Thompson <kst-u@mib.org> - 2012-01-29 02:13 -0800
                Re: bitwise operators(info) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-29 14:59 +0000
                Re: bitwise operators(info) pete <pfiland@mindspring.com> - 2012-01-29 10:51 -0500
                Re: bitwise operators(info) ImpalerCore <jadill33@gmail.com> - 2012-01-30 10:25 -0800
                Re: bitwise operators(info) Ceriousmall <divadsmall@gmail.com> - 2012-01-31 06:00 -0800
                Re: bitwise operators(info) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 14:30 +0000
                Re: bitwise operators(info) pete <pfiland@mindspring.com> - 2012-01-31 10:06 -0500
          Re: bitwise operators(info) Harald van Dijk <truedfx@gmail.com> - 2012-01-28 12:34 -0800
      Re: bitwise operators(info) Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-27 22:56 -0500
        Re: bitwise operators(info) Keith Thompson <kst-u@mib.org> - 2012-01-27 20:30 -0800
          Re: bitwise operators(info) Shao Miller <sha0.miller@gmail.com> - 2012-01-29 09:27 -0500
        Re: bitwise operators(info) Kaz Kylheku <kaz@kylheku.com> - 2012-01-28 05:16 +0000
  Re: bitwise operators(info) Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-01-27 21:44 -0700

csiph-web