15-213 Lab #1 Q&A


Monday, January 28, 2002

Question:

If I work in a team with one other person for lab 1, do we each have to submit a copy of the solutions? Or is one copy per team sufficient?

Answer:

One submission per group is all that we need.


Sunday, January 27, 2002

Question:

I notice some references to "15213-f01", should these be "15213-s02"?

Answer:

Yes. We're very sorry about that. The puzzles were changed since last semester, but it looks like, in the process of editing things, we missed some of the places where the semester was hard-coded. The materials were corrected -- but unfortunately it was too late for those who got copies early. You might want to pull down a new copy and use the new version of the Makefile. We'll do better in the future.


Question:

I have no idea how to get started on reverseByte. do I want to switch just the hex around? am I allowed to use separate storage ints to store intermediate phases of it? how do you recommendI go about it?

Answer:

For reverseByte, you can't just switch the digits of the hex around. Since each digit in hexadecimal represents 4 bits and you want to reverse by the byte, you must reverse the number by every 2 digits of hex.

For example,

reverseByte(0x12345678) = 0x78563412

Yes, you can store intermediate values in your function. If you want to store a step of your computation in a temporary int, that's fine. Just make sure to return the proper value at the end. As for how you go about this one, think about first how you would use the shift operators.


Question:

ok, so I started isNonZero. so far what I have is a tree of or's- or-ing the first 16 bits with the second 16 bits, then 8, then 4, all the way down to one. the good news is it returns the right answer. the bad is that it uses 12 operations, not 10. do you recommend taking a different approach, or is there something I could do to simplify this one?

Answer:

I think there's probably a better approach out there. Be creative!


Saturday, January 26, 2002

Question:

Just wanted to double check.. you are only counting the operators and not equals sign or assignments (ie int a = 4)?

Answer:

No. The assignements are not counted.

Friday, January 25, 2002

Question:

Is fitsBits(x, 0) a valid test case for fitsBits?

Answer:

No. The second parameter of the function must be at least 1 and no greater than 32.


Question:

I wrote the following code. What's the problem?
        int b2 = (mask2 & x) << 8;
        ret = ret | b2;	/* THIS LINE IS THE ONE GIVING ME PROBLEMS! */
        int b3 = (mask3 & x) >> 8;
   

Answer:

The problem is that, in C, although it is okay to initialize variables when you declare them, they must be declared "on top", before any other tyoe of real code.

Look at what you've got here, we see that you declare (and initialize) "b2" and "b3". But, in between, you manipulate the previously declared variable "ret" by assigning it to "ret | b2". This makes the following declaration of b3 illegal. I know it is kind of subtle, but we need to draw the distinction between the initialization that goes on when the variable is declared and any manipulation that happens thereafter.


Question:

What should "dlc" output if the code follows the rules?

Answer:

It should be quiet, indicating no problems.


Question:

When I type "make btest", I get "bash: make: command not found". Why is that?

Answer:

If make doesn't seem to be working, you probably need to add /usr/ccs/bin to your path.


Question:

What is the commands I need to compile and run my bits.c? I tried different ones, but it shows error message :
      Undefined   First referenced
      symbol   in file
      main     /user/local ********
      ld:fatal:symbolic referencing errors. No output written to a.out
      collect 2: ld returned 1 exit status
   

Answer:

Please try using the provided Makefile. The main() function isn't actually in "bits.c". Instead, it lives in "btest.c". "bits.c" really just holds the bitwise functions that you are asked to implement, plus some stuff needed for administrative purposes. The Makefile will build all of the parts for you and link them together to form the "btest" executible.

Thursday, January 24, 2002

Question:

For one of the puzzles, the write-up says:
     isNonZero(x) x!=0 without using ! or &
   
But the comment in the source code says:
   * isNonZero - Check whether x is nonzero using
   *              the legal operators except !
   *   Examples: isNonZero(3) = 1, isNonZero(0) = 0
   *   Legal ops: ~ & ^ | + << >>
   

So is the & operator legal or not legal?

Answer:

It can actually be done either way within the prescribed number of operations, and it really doesn't change the problem very much.

Go ahead and do it with or without the &.


Question:

I'm having some trouble figuring out how to do the leastBitPos function for the lab. Any suggestions?

Answer:

Think about the properties of bit-wise integer addition with respect to the carry-out from each bit-wise add. Under what circumstances will this carry-out occur? Under what circumstances will it "spill over" and cause a carry that otherwise wouldn't have happened? Under what circumstances will a carry-in be absorbed (a bit-wise addition, including the carry-in, does not cause a carry-out)?


Question:

Does carry-out or carry-in mean that a value is being pushed over to the next power after an addition?

Answer:

Yes. After an add overflows, a 1 is "carried out" and placed into the next column. From the perspective of the next addition, this value is "carried in".

The carry-0out is 1-place of overflow on the output of the operation. The carry in is an input, in addition to the normal operands, resulting from the previous addition.


Sunday, January 20, 2002

Question:

In bits.c, it says that we may assume that the machine "has unpredictable behavior when shifting an integer by more than the word size." Does this mean I cannot assume that by shifting an integer 32 bits to the left, I will end up with 0x00000000?

Answer:

Given a 32-bit type, you should not right-shift by more than 31 positions. If you do, the results are, in fact, non-deterministic. In general, the results of >> are indeterministic for shifts equal to or wider than the data type.

So, right-shifting by 32 positions is not guaranteed to give you 0. And, in fact, in many cases it won't.


Question:

Does the same [non-deterministic if shifted more than 31] apply to left shift (<<)?

Answer:

Yes.