# C-language binary Math - Convert bytes to hex and to decimal

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?)

1. Given two bytes x=0x23 and 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 = 34 and the second row is 0xfe. There are two ways to interpret 0xfe, either as a uint8_t, in which case its decimal value is 254, or an int8_t, in which case its decimal value is -2. The problem is ambiguous! It describes the data types of x and y as simply "bytes". I think you're justified in assuming an unsigned type, so your 254 is correct. If possible, I would explicitly write this assumption in your homework. You can even mention that -2 is the other option, just to cover your bases in case the grader makes the opposite assumption.

0xfffffffe is 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!

First, in your example, ~x|y=0xffff fffe instead of 0xfe -- the leading 1's make all the difference.

Are you familiar with the two's complement representation of numbers? Basically, 0xffff ffff is -1 in decimal. 0xffff fffe is 1 less than 0xffff ffff and is therefore -2.

• Alright...Interesting...Never heard of Two's Complement. Why does it apply here? How did we evaluate 0xffff fffe to be the hex equivalent of ~x | y in the first place? As seen above, I evaluated ~x|y=0xfe=254. I don't know why that's wrong. I used the same style of logic to get that answer as I did with my evaluation of x & y, which the tutor's answer agrees with. I don't know why I'm supposed to smack on 7 additional leading f's and treat it as a "two's complement" hex value. – wad11656 Oct 17 '17 at 4:56