Operate with binary digits

FlamingClaw 0 Tallied Votes 196 Views Share

I want to show you some rules about bit operating like Not,And,Or,Xor,Shl,Shr...
By FlamingClaw

Program LogicalOperation;

{used moduls}
Uses Crt;

{Declare variables}
Var   a,b,c:Byte;{0..255}


Begin {main}
     ClrScr; {ClearScreen}

     {Decimal}  {Binary}
     a:=19;     { 00010011 }
     b:=3;      { 00000011 }
{
     -= RULE =-        -= a =-              -= b =-
     1*2^7 =   128     0*2^7 =     0        0*2^7 =     0
     1*2^6 =    64     0*2^6 =     0        0*2^6 =     0
     1*2^5 =    32     0*2^5 =     0        0*2^5 =     0
     1*2^4 =    16     1*2^4 =    16        0*2^4 =     0
     1*2^3 =     8     0*2^3 =     0        0*2^3 =     0
     1*2^2 =     4     0*2^2 =     0        0*2^2 =     0
     1*2^1 =     2     1*2^1 =     2        1*2^1 =     2
     1*2^0 = +___1     1*2^0 = +___1        1*2^0 = +___1
               255                19                    3
 }

{-= RULES WITH EXAMPLES =-}

     {-= NOT =-}
     {every bit will turn about}
     c:= Not a;
     {00010011 --> 11101100 = 236}
     WriteLn(c);

     {-= AND =-}
     {where both of place is 1 there will be 1 else 0}
     c:= a And b;
     {         **
     a = 00010011
     b = 00000011
     c = 00000011 = 3  }
     WriteLn(c);

     {-= OR =-}
     {where both of place is 0 only there will be 0}
     c:= a Or b;
     {   *** **
     a = 00010011
     b = 00000011
     c = 00010011 = 19  }
     WriteLn(c);

     {-= XOR =-}
     {where the bits are different there will be 1}
     c:=a Xor b;
     {      *
     a = 00010011
     b = 00000011
     c = 00010000 = 16  }
     WriteLn(c);

     {-= SHL =-}
     {shift by bit to left with the added number}
     c:=a Shl b;
     {  <--*
     a = 000|10011 with 3
     c = 10011000 = 152
     }
     WriteLn(c);

     {-= SHR =-}
     {shift by bit to right with the added number}
     c:=a Shr b;
     {         *--->
     a = 00010|011 with 3
     c = 00000010 = 2
     }
     WriteLn(c);


     ReadKey;{wait for press any key}
End. {main}
{
-=Note By FlamingClaw=-
I hope I can help you...
-=Created By FlamingClaw=-
2009.03.17.
}