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. What's the word size on this hypothetical computer?

3. 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.)

4. 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.

5. 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. If n=1010 (decimal 10) these statements don't make any sense (to me anyways).
Originally Posted by joatmon
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. Originally Posted by itCbitC
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.

8. 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. 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.

10. Originally Posted by joatmon
Assuming the same 4-bit computer
What is meant by this!!

Tim S.

11. 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. Somewhere in the book it described a 4-bit computer; what was the description?

Tim S.

13. "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. Originally Posted by stahta01
n=1010 does not equal ten remember it is signed 4 bit binary.
Originally Posted by stahta01
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. Originally Posted by itsme86
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.