1

Hey I have been having trouble with a C program. The program I have to write simulates the operation of a VAX computer. I have to take in 2 variables x and y to generate z. within that there are two functions, the first

  • Sets Z to 1 where each bit position of y = 1
  • 2nd sets z to 0 where each bit position of y = 1

I'm not asking for someone to do this for me, I just need an explanation on how this is carried out as I have a bare bones of the two functions that I need. I was thinking of something like this but I don't know if it's right at all.

#include<stdio.h>

int main()
{
   int x1 = 1010;
   int y1 = 0101;

   bis(x1, y1);
   bic(x1, y1);
}

/* BIT SET function that sets the result to 1 wherever y = 1 */
int bis (int x, int y)
{
   int z = x & y;

   int result = ?;
   printf("BIT SET: \n\n", result);
   return result;
}

/* BIT CLEAR function that sets result to 0 wherever y = 1 */
int bic(int x, int y)
{
   int z = x & y;

   int result = ?;
   printf("BIT CLEAR:\n\n ", result);
   return result;
}

Apologies for the poor naming conventions. Am I anyway on the right track for this program?

PyGuy91
  • 78
  • 9
  • 8
    Those are not binary numbers, and `int y1 = 0101;` is probably not the number you think it is. – Marlon Mar 02 '11 at 22:33
  • 2
    Could you be a little more precise about the operation semantics? Are you familiar with bitwise or, and, not? I believe what you need might just be `z = x | y;` (x or y) and `z = x & (~y)`. (x and not y) – Zulan Mar 02 '11 at 22:38
  • I'm sorry for not declaring the variables properly, I have been doing bitwise operations for the past 2 weeks including bit shifting but as far as checking the bit positions I have never touched on that or is doing the ANDING going to give me the desired results? I apologise but I am having some difficulty with bitwise stuff – PyGuy91 Mar 02 '11 at 22:45
  • Could you give an example of the inputs and output of your functions? It's really not clear what you're asking for. – Karmastan Mar 02 '11 at 22:54
  • Both functions take in a data word 'x1' and a mask word 'y1', which generate a result 'z' with the bits of x1 modified according to y1. then the Bit set function sets z to 1 at each bit position where y=1 and bit clear sets z to 0 at each bit where y = 1. That's all the information I have been given – PyGuy91 Mar 02 '11 at 22:58
  • Another thing no one mentioned - you are not using your input to printf... – BlueRaja - Danny Pflughoeft Mar 02 '11 at 23:16
  • "Sets Z to 1 where each bit position of y = 1": this is simply Z=y. There's something missing from the description... the role of "x", perhaps? Should the description of the first operation be "Set a bit of Z to the value of the corresponding bit of X when the corresponding bit of Y is 1, else set the bit to 0"? – outis Mar 03 '11 at 00:44

2 Answers2

3

Let's look at bitset() first. I won't post C code, but we can solve this on paper as a start.

Say you have your integers with the following bit patterns: x = 1011 and y = 0101. (I'm changing your example numbers. And, incidentally, this is not how you would define two integers having these bit patterns, but right now we're focusing on the logic.)

If I am understanding correctly, when you call bitset(x, y), you want the answer, Z, to be 1111.

x = 1011
y = 0101
     ^ ^-------- Because these two bits have the value 1, then your answer also
                 has to set them to 1 while leaving the other bits in x alone.

Well, which bitwise operation will accomplish this? You have AND (&), OR (\), XOR (^), and COMPLEMENT (~).

In this case, you are ORing the two values. Looking at the following truth table:

x        1 0 1 1
y        0 1 0 1
-----------------
(x OR y) 1 1 1 1

Each bit in the last row is given by ORing that column in x and y. So (1 OR 0) = 1, (0 OR 1) = 1, (1 OR 0) = 1, (1 OR 1) = 1

So now you can write a C function bitset(x, y), ORs x and y, and returns the result as Z.

What bitwise operator - and you can do it in multiple steps with multiple operators - would you use to clear the bits?

                                x  1 0 1 1
                                y  0 1 0 1
-------------------------------------------
(SOME OPERATONS INVOLVING x and y) 1 0 1 0

What would those logical operators (from the list above) be? Think about the "and" and "complement" operators.

Good luck on your hw!

Bonus: A quick primer on expressing integers in C.

int x = 1337 creates an integer and gives it the value 1337. If you said x = 01337, x WILL NOT have the value "1337" like you might expect. By placing the 0 in front of the number, you're telling C that that number is in octal (base 8). The digits "1337", interpreted in base 8, is equivalent to decimal (base 10) 735. If you said x = 0x1337 then you are expressing the number in base 16, as a hexadecimal, equivalent to 4919 in base 10.

poundifdef
  • 18,726
  • 23
  • 95
  • 134
  • Thanks rascher for that response. It's been a great help. I am hoping this is as tough as it gets with this part of my comp arch module – PyGuy91 Mar 02 '11 at 23:22
0

Nope... what you have there will and together two integers. One of which is 1010 (base10), and the other of which is 101 (base 8 - octal -> 65 base 10).

First you'll want to declare your constants as binary (by prefixing them with 0b).

Second, you'll want to out put them (for your instructor or TA) as a binary representation. Check out this question for more ideas

Community
  • 1
  • 1
BIBD
  • 15,107
  • 25
  • 85
  • 137
  • Note, you may or may not run into issues with Two's complement representation of integers - it's been so long since I've had to worry about that. – BIBD Mar 02 '11 at 22:45
  • I think prefixing constants with `Ob` isn't standard. It's supported by GCC as an extension though. – fouronnes Mar 02 '11 at 23:02