Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 29 Jan 2012 09:51:19 -0600 Message-ID: <4F256AF6.2BCB@mindspring.com> Date: Sun, 29 Jan 2012 10:51:18 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: bitwise operators(info) References: <96da10c2-5084-44fe-a0b1-dfd3d145e36f@eb6g2000vbb.googlegroups.com> <42dacbf3-7657-44e2-b9b8-1be8e49651f8@t8g2000yqg.googlegroups.com> <0.e41cbc1eedd06ab4387e.20120128113315GMT.874nvg3vus.fsf@bsb.me.uk> <0.2835a3ac4e2fe672524d.20120128224118GMT.87r4yj30xd.fsf@bsb.me.uk> <5275606b-2eef-4e1d-9bd5-7ad2b454778e@x6g2000pbk.googlegroups.com> <0.39c22a4cda991bf5217b.20120129145931GMT.874nve367g.fsf@bsb.me.uk> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 73 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.221.5 X-Trace: sv3-brHJPDSXCH68UAgaiqyQ/vAbi9+A8woeFzY47WeiRzF3UhYenuS3MdSXJ+8OIO3meZYl94Y2BX5Ca/6!KT/JJ+ans3fpFPdXwr8qeOXLvv/SgPK+HiPhxn+JZKY02/wdqwCtkIc1N4RFqYAt4AkSXEUNCA3h!T8cP6JCs 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: 3897 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:16393 Ben Bacarisse wrote: > > Ceriousmall 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