misunderestanding of logical operator of c language
I have such these questions in my book that I have do it for checking my self but I don't know the correct answer and there is no any ex开发者_Go百科planation.
it has two parts "a" and "b"
a) A = B & C[0]
b) A = A ? B : C[0]
C[0] = 0x00001234
A = 0x00000000
B = 0x00002222
it want from me that
1)what is the result of A
2) the MIPS instruction for each part
3)show the bit level representation of each in structure.
thanks
&
is a bit-wise AND. Each bit of the output is 1 if both corresponding input bits are 1:
0x00001234 = .... 0001 0010 0011 0100
0x00002222 = .... 0010 0010 0010 0010
AND result = .... 0000 0010 0010 0000 = 0x00000220
I don't know MIPS but the instruction is probably just called and
.
?
is the ternary operator: your example means "if A is a true boolean expression then return B else return C[0]". Generally in C false is zero and true non-zero (I'm not 100% sure if this is standardised - I don't think it was in the earlier standards, but it's common uses) so
- if A = 0x0000 then (bool)A == false and the result is C[0].
- if A = 0x0220 (carrying forward the result from part a) then (bool)A == true and the result is B.
&
is the bit-wise AND operator in C.
eg.
0000 & 0001 = 0000
?
is another way of writing if
A = A ? B : C[0]
says that A gets the value = (if A is true then B ELSE C[0])
Convert for Hex to Binary and do the exercise.
edit : clarify : made "AND" to -> "bit-wise AND"
the mips:
a) A = B & C[0]
$t1 = C
$t2 = B
$t3 = A
lw $t0, 0($t1) ;move c into temp reg
and $t3, $t0, $t2 ;perform bitwise and
b) A = A ? B : C[0]
$t1 = C
$t2 = B
$t3 = A
beq $0, $t3, 3 ;if A is 0 go 3 lines down
add $t3, $0, $t2 ;move B to A
j end ;goto the end
lw $t0, 0($t1) ;load c into a temp reg
add $t3, $0, $t0 ;move temp value into a
end:
精彩评论