Thread: Really confused on how to attack this question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Really confused on how to attack this question

    Hello everyone, im preparing for my first c exam and going through some practice questions.

    Display the appropriate code to reset to 0 the middle two bits of variable x who type is unsigned char.

    Im really confused on how to start. Im assuming that i need to convert the variable x into binary, and then use some-type of bit operator to shift through the code and replace the appropriate values. Any suggestions would be appreciated, thanks.
    Last edited by purplehaze; 02-28-2010 at 08:43 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you mainly need to use bitwise and. No "conversion to binary" is needed, since you already have control over the bits with bitwise operations and assignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That would work, but it's not what they're looking for. They want a mask, that when AND'd with the unsigned char byte, will clear out those two middle bits.

    Check into those two terms.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The most portable way to do this is probably going to involve the one's complement operator to set all bits to 1 first, and then use bit shifting to create a XOR mask. I'm not sure if the number 0 is stored as 11...1 or 00...1 on systems that use one's complement instead of two's complement though.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Memloop
    The most portable way to do this is probably going to involve the one's complement operator to set all bits to 1 first, and then use bit shifting to create a XOR mask.
    Sounds good (i.e., use bitwise not on 0), but I still think that bitwise and is the one to use since the other bits could just be ANDed with 1.

    Quote Originally Posted by Memloop
    I'm not sure if the number 0 is stored as 11...1 or 00...1 on systems that use one's complement instead of two's complement though.
    This concerns an unsigned char, so we do not need to worry about signed integer representation
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    > Sounds good (i.e., use bitwise not on 0), but I still think that bitwise and is the one to use since the other bits could just be ANDed with 1.

    But you're going to have to hard code the bitmask, and that's going to be trouble if CHAR_BITS change. What are you going to do on machines that only have 16 bit bytes?

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    x &= 0347 is the solution. I know how the and operator works, but im confused what exactly are we anding with. I also assume that the value 0347 is a random bit string used for this problem.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    0347 is octal for 11100111.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Memloop
    But you're going to have to hard code the bitmask, and that's going to be trouble if CHAR_BITS change. What are you going to do on machines that only have 16 bit bytes?
    You do not have to hard code the bit mask, but after thinking more carefully on how I would actually do it, let me guess: when you say "use bit shifting to create a XOR mask", you actually mean to use bit shifting and bitwise xor to create the mask that will be used with bitwise and? That seems to be the simplest solution that I can come up with at this point, e.g.,
    Code:
    unsigned char mask = 3U << (CHAR_BIT / 2 - 1);
    mask ^= ~0U;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Yes that's exactly what I had in mind. I'm guessing 0 is always interpreted as +0 even if you are using signed numbers (a lot of existing code would be broken otherwise)?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Memloop
    I'm guessing 0 is always interpreted as +0 even if you are using signed numbers (a lot of existing code would be broken otherwise)?
    That certainly appears to be the case. C99 states that:
    Quote Originally Posted by C99 Section 6.2.6.2 Paragraph 3
    If the implementation supports negative zeros, they shall be generated only by:
    • the &, |, ^, ~, <<, and >> operators with arguments that produce such a value;
    • the +, -, *, /, and % operators where one argument is a negative zero and the result is zero;
    • compound assignment operators based on the above cases.

    It is unspecified whether these cases actually generate a negative zero or a normal zero, and whether a negative zero becomes a normal zero when stored in an object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Thanks for the responses guys, im still trying to make sense of all of this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM