Thread: Exercise 2-6 K&R help

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by allix
    ok.. to give another scenerio

    temp <<= p -2; // set to n bits of y at position p (temp = 00000100)
    would that be correct?
    It doesn't set anything. It shifts 'temp' over to the left 'p - 2' times. So if p were 4, and temp were 1, then your results would be 00000100.

    Quote Originally Posted by allix
    mask <<= p - 2; // n bits set at position p (mask = 00000110)
    also, would that be correct?
    If p were 3, and mask were 3, yes.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Aug 2006
    Posts
    9
    Code:
     x &= ~mask;         // n bits at position p cleared (x = 10010011) //
    quzah , how did that work?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just break it down into steps.

    x = ( x & ~mask );

    Take X and & it with the results of inverting all bits in mask.
    Assign that to X.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Aug 2006
    Posts
    9
    Quote Originally Posted by quzah
    Just break it down into steps.
    x = ( x & ~mask );
    Take X and & it with the results of inverting all bits in mask.
    Assign that to X.
    Quzah.
    do you mean the following?

    so x=
    10010011
    mask inverted =
    11110011
    & =
    10010011

  5. #20
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by allix
    do you mean the following?

    so x=
    10010011
    mask inverted =
    11110011
    & =
    10010011
    Exactly!
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  2. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM
  5. k&r exercise 1-9
    By xion in forum C Programming
    Replies: 14
    Last Post: 07-15-2003, 06:01 PM