07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1 ■ Introduction to Cryptography and Python 15

Understanding Bitwise Operators

The bitwise operators work on bits and perform bit-by-bit operations (see Table 1.6).

Assuming a = 60 and b = 13, the binary format of a and b will be as follows:

a = 0011 1100

b = 0000 1101

Table 1.6: Bitwise Operators

OPERATOR DESCRIPTION EXAMPLE

& (binary AND)

Copies a bit to the result if it exists in

both operands.

| (binary OR) Copies a bit if it exists in either

operand.

^ (binary XOR)

~ (binary One

Complement)

<< (binary Left

Shift)

>> (binary Right

Shift)

Copies the bit if it is set in one operand

but not both.

This operator is unary and has the

effect of “flipping” bits.

The left operand’s value is moved left

by the number of bits specified by the

right operand.

The left operand’s value is moved

right by the number of bits specified

by the right operand.

(a & b) (means 0000

1100)

(a | b) = 61 (means

0011 1101)

(a ^ b) = 49 (means

0011 0001)

(~a ) =-61 (means 1100

0011 in two’s

complement form due to

a signed binary number.

a << 2 = 240 (means

1111 0000)

a >> 2 = 15 (means

0000 1111)

Understanding Membership Operators

Membership operators test for membership in a sequence, such as strings, lists,

or tuples. There are two membership operators, in and not in, as shown in

Table 1.7.

Table 1.7: Membership Operators

OPERATOR DESCRIPTION EXAMPLE

in

not in

Evaluates to true if it finds a variable in

the specified sequence and false

otherwise.

Evaluates to true if it does not find a

variable in the specified sequence and

false otherwise.

x in y; here in results in a

1 if x is a member of

sequence y.

x not in y; here not in

results in a 1 if x is not a

member of sequence y.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!