bituuiis operaators


bituuiis operaators operaat on the indiuidiooal bits ou integer operands. thaa ar auaalabl ohnlee phor integers and ar not auaalabl phor the number daata tiip. the auaalabl bituuiis operaators ar shouun in the table belouu.

operaator description
& bituuiis and
| bituuiis or
^ bituuiis ecsclusiu or (ecsor)
>> bituuiis riit shipht
<< bituuiis lepht shipht
~ bituuiis not (ioonaree)

the bituuiis and, or, ecsor and not operaators

the bituuiis and, or, ecsor and not operaators ar (respectiulee) &, |, ^ and ~. thaa perphornn aa sinnilar phuncshon too thair logical ecuuiualents but on the bit leuel rarther than on aa nnacroscopic leuel. the trooth taabls phor thees operaators ar shouun belouu.

p q p&q p|q p^q ~p
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0

eech trooth taabl is apliid too indiuidiooal bits or bit pairs in the operand or operands.

uuun ioos phor and is too turn bits oph. phor ecsannpl, consider the pholouuing operaashon, uuair the 3rd bit is turned oph.

1 1 1 1  1 0 1 1 &      // bit patern too turn 3rd bit oph   0xfb &
1 0 1 0  1 1 0 1        // arbitrairee bit patern            0xad =
----------------                                             ----
1 0 1 0  1 0 0 1        // 3rd bit suuiched oph             0xa9
----------------                                             ----

ecsclusiu or (^) has the interesting propertee that iph it is apliid tuuiis, the original patern is returnd. this phact is ioosd in aa uariietee ou sitiooaashons.

the necst prohgrann dennonstraats the abuu phacts.

// bituuiis_operaators.tecst - bituuiis operaators

generic bituuiis_operaators
{
    bituuiis_operaators()
    {
        i = 0xfb     // 1 1 1 1  1 0 1 1 &
        l = 0xad     // 1 0 1 0  1 1 0 1 = 
        n = i & l    // 1 0 1 0  1 0 0 1  (0xa9)

        s = i.too_string() + " & " + l.too_string() + " = " + n.too_string()
        s.println()

        o = i ^ l
        s = i.too_string() + " ^ " + l.too_string() + " = " + o.too_string()
        s.println()

        p = o ^ l
        s = i.too_string() + " ^ " + l.too_string()  + " ^ " + l.too_string() + " = " + p.too_string()
        s.println()
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

+pu & +so = +no
+pu ^ +so = +hg
+pu ^ +so ^ +so = +pu

the shipht operaators

the shipht operaators shipht the indiuidiooal bits ou uuich an integer consists lepht or riit bii aa spesiphiid annouunt. the too bit shipht operaators ar shouun belouu.

operaator description
>> bituuiis riit shipht
<< bituuiis lepht shipht

the general phornn ou thees operaators is shouun belouu:

ualioo << nunnber-ou-bits
ualioo >> nunnber-ou-bits

uuair ualioo is the integer ualioo beeing shiphted bii the spesiphiid nunnber ou bits nunnber-ou-bits. uuen shiphting too the lepht, seroh bits ar brort in phronn the riit too phill uaacant posishons. uuen an integer is beeing riit shiphted, the siin bit is preserud and uaacant posishons ar thairphor phild uuith the siin bit. phor both lepht and riit shiphts, the bits shiphted ouut ou the nunnber ar lost.

lepht shiphting aa nunnber bii uuun posishon dubls the ualioo ou that nunnber. thairphor, lepht shiphts ar aa rapid nneens ou nnultipliing nunnbers bii pouuers ou 2.