if ((status & 0x3F) == 1 ){ }..the status is variable in swift language.what is mean about this condition, & mean and (status & 0x3F) value return
Best Answer
&
is the bitwise AND operator. It compares the bits of the two operands and sets the corresponding bit to 1
if it is 1
in both operands, or to 0
if either or both are 0
.
So this statement:
((status & 0x3F) == 1)
is combining status
with 0b111111
(the binary equivalent of 0x3F
and checking if the result is exactly 1
. This will only be true if the last 6 bits of status
are 0b000001
.
In this if
:
if( (dtc24_state[2] & 0x8) == 0x8 ) {self.haldexABCDTC24State.text = status_str + " - UNKNOWN"self.haldexABCDTC24State.textColor = text_coloractive_or_stored_dtc = true}
dct24_state
is an array of values. The value of dct24_state[2]
is combined with 0x8
or 0b1000
and checked against 0x8
. This is checking if the 4th bit from the right is set. Nothing else matters. If the 4th bit from the right is set, the if
is true and the code block is executed.
0x3F is 111111
. So, it means this:
for each bit of yourNumber in binary system presentation use and
method. This way truncates the left part of the number. and the result compares with 1
.
e.g.
7777
is 1111001100001
after executing and
this number converts into
100001
. So the result is false
.
But for 7745
(1111001000001
) the result is 1
. The result is true
.
The rule for 'and' function: 0 & 0 = 0 ; 0 & 1 = 0; 1 & 0 = 1; 1 & 1 = 1.