Bitwise Operators

Bitwise Operators -- (<<, >>, ~ , &, |, ^)

Syntax

number << shift;
number >> shift;
~ number
number & number
number | number
number ^ number

		

Arguments

number

Any number,

shift

The number of bit shifts to perform.

Returns

An integer which is the result of the particular operation.

Description

<<, >> return the first argument with a left or right bitshift operation performed the number of times of the second argument.

~ returns the binary opposite of the number.

& compares each of the corresponding digits of the two numbers. If both digits are 1, returns 1 for that place. Otherwise returns 0 for that place.

| compares each of the corresponding digits of the two numbers. If either those digits is 1, returns 1 for that place. Otherwise returns 0 for that place.

^ compares each of the corresponding digits of the two numbers. If both digits are the same, returns O for that place. If they are different (ie. 0 and 1) returns 1 for that place.

Examples

    Gamma> bin(10);
    0b1010
    Gamma> bin(10 << 1);
    0b00010100
    Gamma> bin(10 >> 1);
    0b0101
    Gamma> bin (~10);
    0b11111111111111111111111111110101
    Gamma> bin(10);
    0b1010
    Gamma> bin (9);
    0b1001
    Gamma> bin (9 & 10);
    0b1000
    Gamma> bin (9 | 10);
    0b1011
    Gamma> bin (9 ^ 10);
    0b0011
    Gamma> 
    		

See Also

band, bnot, bor, bxor

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.