I am having concerns on a homework assignment.
All problems are in C-Language Syntax (<--What does that imply? Am I supposed to write my answers in a certain format?)
- Given two bytes x=
0x23and y=0xA2, fill in the following table indicating the byte values of the different C expressions: (ONE BYTE ANSWERS) (<--What does that imply? What is a "one-byte" answer?)-------------------------------------------- | Expression Hex Value Decimal Value | | x & y | | ~x | y | | !x || y | | x && ~y | --------------------------------------------
I have looked up the binary equivalencies of 0x23 and 0xA2. I found that:
0x23=0010 0011=23=x
and
0xA2=1010 0010=162=y
I know that the & symbol is the AND operator and | is the OR operator. Knowing this, I ANDed the binary values of x and y together to find x & y:
x: 0010 0011 y: 1010 0010 x & y: 0010 0010
0010 0010=0x22=34=x & y
...and OR'd the vaues of ~x and y together to find ~x | y:
~x: 1101 1100 y: 1010 0010 ~x | y: 1111 1110
1111 1110=0xfe=254=~x | y
Are those correct?
I asked an online tutor to assist me, and he got the following results:
------------------------------------------- | Expression Hex Value Decimal Value | | x & y 0022 34 | | ~x | y ffff fffe -2 | | !x || y 0001 1 | | x && ~y 0001 1 | -------------------------------------------
How did he get -2 as the decimal value for ~x | y? Is getting negative values even possible? Also, are his hex values formatted correctly? In the instructions, it says to give "ONE BYTE ANSWERS". Doesn't that mean they should be formatted like they are in the question? (i.e. 0x23 and 0xA2).
Finally, that leaves the && and || operators. On this page, it says that && and || are "logic operators", while & and | are bitwise operators. I'm not sure what that means or how to apply that to the evaluation of the binary expressions.
By "C-Language Syntax", the problem presumably means that all the symbols are to be interpreted as given in the C standard. This helps to clarify, for example, the distinction between
|and||.You're right, the first row is
0x22 = 34and the second row is0xfe. There are two ways to interpret0xfe, either as auint8_t, in which case its decimal value is254, or anint8_t, in which case its decimal value is-2. The problem is ambiguous! It describes the data types ofxandyas simply "bytes". I think you're justified in assuming an unsigned type, so your254is correct. If possible, I would explicitly write this assumption in your homework. You can even mention that-2is the other option, just to cover your bases in case the grader makes the opposite assumption.0xfffffffeis incorrect because it occupies 4 bytes, which violates the "ONE BYTE ANSWERS" part of the question.As for the logical operators, their behavior is not something that you can divine from mathematical principles; it really depends on how C represents booleans as integers, which is not universal among programming languages. Your class really ought to cover that stuff!