Like Tree1Likes

Masking Operation

This is a discussion on Masking Operation within the C Programming forums, part of the General Programming Boards category; I have two questions that I am trying to solve: Assuming the same 4-bit computer with signed representation, give a ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Masking Operation

    I have two questions that I am trying to solve:

    Assuming the same 4-bit computer with signed representation, give a C expression that computes the requested:

    Given a number 'n', compute a word with the lower 'n' bits set to '1'. You don't know 'n' in advance.
    Given a number 'n', compute a word with the higher 'n' bits set to '1'. You don't know 'n' in advance.

    I think that this is a question on masking, and I am totally new to bit operations (and to C altogether). Our book gives one whole paragraph to the topic of masking, and our instructor glossed over it so fast that if you coughed, you missed it. I've been searching all over the place to figure this out. Basically, I am totally clueless.

    Can somebody help me with this? I don't know where to look for information or even how to start.

    Thanks a lot! Sorry to be so dense.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    What's the word size on this hypothetical computer?
    stahta01 likes this.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    I'm going to go with:
    Code:
    0xFu >> (4 - n);
    and...
    Code:
    0xFu << (4 - n);
    ... which have nothing to do with masking, but shifting.

    Basically, what you're doing is starting with all 1s and shifting the unwanted ones off the end. So let's say 'n' was 3. You start off with 0xF (1111) and shift it 4 - 3 times to the left which leaves you with 0xE (1110) because the left-most 1 fell off the end and the shift operation stuck a 0 in the right-most position. You can see how shifting right would do the same thing but leave you with 0s in the left-most positions instead.

    (the 'u' constant suffix should force the compiler to treat 0xF as an unsigned value instead of a signed value to avoid instances where a 1 might get shifted in from the left on a right-shift operation when the most significant bit is a 1, although it's probably unnecessary.)
    Last edited by itsme86; 08-31-2011 at 02:56 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    No that's not quite right. I agree that you want to shift though. This isn't normal masking, since n isn't known.

    The two shift operators, >> and << will, when used on an unsigned type, shift the binary representation of the number by 1 bit, removing a bit from one end, and adding 0 to the other. You could also make use of the operator ~, which flips each bit, setting 0 to 1, and 1 to 0.

    On the other hand, since it's a 4 bit word, there are only 3 possible values for n, not counting the trivial. So you could do it as a switch statement with normal masking.
    Converting certain bits to one is my applying the bitwise or operator, |, between the value to mask and a mask which has whatever values you want set in the result to be set. For completeness I'll also mention that to clear certain bits you instead apply the bitwise and operator, &, the same way, with the mask having 0 for the bits you want to clear.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Thanks. This is really helpful and gets me started.

    I want to make sure that I understand what you are doing. The 0xFu part sets up a 4-bit number '1111'. Then, the '<< (4 - n)' part shifts that number over so that the left 'n' bits are now '1' and all bits to the right of that are '0' . Did I get that right?

    Assuming that I understand this correctly, the only problem that I see with it is that the initial problem says "given a number 'n'...". With what you are suggesting, I think that you aren't accepting a given 'n', but rather imposing 'n = 1111'. What if 'n' was given as '1010' with n = 3? Shouldn't I wind up with '1111'? That's why I was looking at this as a masking problem, not a shifting one.

    What do you think? Am I looking at this correctly or am I still missing something. As I mentioned, this is the first time that I have ever tried something like this, so I'm almost certainly missing something.

    Thanks for your help. I really appreciate it.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    If n=1010 (decimal 10) these statements don't make any sense (to me anyways).
    Quote Originally Posted by joatmon View Post
    Given a number 'n', compute a word with the lower 'n' bits set to '1'. You don't know 'n' in advance.
    Given a number 'n', compute a word with the higher 'n' bits set to '1'. You don't know 'n' in advance.
    The above says that given n=10, compute a word with the lower 10 bits set to 1?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Quote Originally Posted by itCbitC View Post
    If n=1010 (decimal 10) these statements don't make any sense (to me anyways).

    The above says that given n=10, compute a word with the lower 10 bits set to 1?
    n=1010 does not equal ten remember it is signed 4 bit binary.
    So, n=0111 or seven would be the largest value in signed 4 bit binary.

    Edit: Your posted question implies n must be a value of 0 to 4; else the question makes little sense.

    Tim S.
    Last edited by stahta01; 08-31-2011 at 05:17 PM.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    That's a good point, although the problem states signed representation, which makes 1010 = -6, I believe. Nonetheless, you are right. If this is a 4-bit computer, does that mean that there is a restriction such that 0 <= n <= 4? Now I am totally confused. Not only about what I need to do, but about what they are even asking of me.

    Using Mir's suggestion, what do you think about this (this is the part when you see that I really know nothing about C - think of this as pseudocode):

    Code:
    switch (n)
    {
       case 4:
           n = n | '1111'
           break;
       case 3:
           n = n | '0111';
           break;
       case 2: 
           n = n | '0011';
           break;
       case 1:
           n = n | '0001';
           break;
    }
    I won't get offended if you laugh out loud. How do I write this so that it will really do what is intended?

    Thanks again.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    First, you need to figure out what is intended.

    Edit: I have no idea how the code you posted in post #8 relates to solving the problem; but, I am not sure what the problem is.

    Tim S.
    Last edited by stahta01; 08-31-2011 at 05:56 PM.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Quote Originally Posted by joatmon View Post
    Assuming the same 4-bit computer
    What is meant by this!!

    Tim S.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Well, if this is a 4-bit computer, that has to mean that 0 <= n <=4, right? So, n has to be '0000' or '0001' or '0010' or '0011' or '0100'. The point of the exercise must be to mask these bits so that the final outcome is '1111', 'x1111', 'xx11', 'xxx1', or 'xxxx' where 'x' is the corresponding digit from the original value of n. Let's just go with that as the goal.

    My terrible code, which I didn't even explain correctly - I meant to use an bitwise OR operator, is meant to look at each case 0 - 4, and mask the input n based on which one of the five outcomes that I want, given the value of n. Does this make sense?

    Unfortunately, I have absolutely no idea how to write C code to make this happen. My book only explains to to mask bytes (not bits) and I can't figure this out. I'm going to do more searching, but any help that you can give is very appreciated.

    Thanks.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Somewhere in the book it described a 4-bit computer; what was the description?

    Tim S.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    "Assuming the same 4-bit computer" just refers back to a previous question. The context is a 4-bit computer with signed 2's complement representation.

    This is the exact text: "Assume you're working on a 4-bit computer that uses a signed 2's complement representation"

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by stahta01 View Post
    n=1010 does not equal ten remember it is signed 4 bit binary.
    Dang! I forgot about 2's complement while asking.
    Quote Originally Posted by stahta01 View Post
    So, n=0111 or seven would be the largest value in signed 4 bit binary.
    Yep! that is correct but even then the OP's question doesn't make any sense.

    > Given a number n (say 7) compute a word with the lower n (7) bits set to 1.

    Kinda confused how that's possible on a machine whose wordsize is just 4 bits?

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Quote Originally Posted by itsme86 View Post
    Code:
    0xFu >> (4 - n);
    and...
    Code:
    0xFu << (4 - n);
    The above code looks good to me; but you will need to apply a 4 bit mask on the left shift answer.
    This assumes you are NOT running the code on the 4 bit computer.

    Code:
    (0xFu << (4 - n)) && 0xFu;
    Note: I have no idea if this is the right problem to be solving.

    Tim S.
    Last edited by stahta01; 08-31-2011 at 07:29 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Masking
    By fiendslyr in forum C Programming
    Replies: 11
    Last Post: 06-25-2010, 02:59 PM
  2. Bit Masking
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 01-08-2007, 02:23 AM
  3. Masking(or ...)
    By suzanne_lim in forum C Programming
    Replies: 5
    Last Post: 03-07-2006, 01:37 AM
  4. Cin Masking
    By Dunners in forum C++ Programming
    Replies: 8
    Last Post: 02-20-2005, 08:14 PM
  5. DIB masking
    By SAMSAM in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2003, 10:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21