#!/usr/bin/python3 # -*- coding: utf-8 -*- """ @author: taylor@mst.edu """ # %% Numeric bases and text encoding """ int(x [,base]) Converts x to an integer. The base specifies the base if x is a string. """ # binary string, base 2 print(int("101101", 2)) """ bin(x) Returns binary representation of integer """ print(bin(45)) """ chr(x) Converts an integer to a character. ord(x) Converts a single character to its integer value. hex(x) Converts an integer to a hexadecimal string. oct(x) Converts an integer to an octal string. """ # Simple low level conversions: print(ord("a")) # letter to integer print(chr(97)) # integer to character print(bin(4)) # integer to binary print(bin(ord("a"))) # letter to binary print(int("0b1100001", 2)) # binary to integer (from base 2) print(int("1100001", 2)) # binary to integer (equivalent to above) print(int("0xBEEF", 16)) # hex to int (from base 16) print(int("ff", 16)) # hex to int (equivalent) print(hex(37)) # int to hex print(hex(int("010101", 2))) # binary to hex # At byte level print(bytes("anystring", "utf-8")) # string to bytes print("anystring".encode()) # string to bytes (same) print(b"anystring".decode()) # bytes to string print(b"anystring".hex()) # bytes to hex print(bytes.fromhex("beef")) # hex to bytes i = 16 print(i.to_bytes(1, byteorder="big", signed=True)) # int to bytes print(int.to_bytes(16, 1, byteorder="big", signed=True)) # int to bytes (same) print(int.from_bytes(b"\x00\x0F", byteorder="big")) # bytes to int print(int(b"\x00\x0F".hex(), 16)) # bytes to int print(bytearray("anystring", "utf-8")) # string to mutable bytes """ Bitwise operators """ a = 5 print(bin(a)) b = 12 print(bin(b)) """ & Binary AND Operator copies a bit, to the result, if it exists in both operands """ print(a & b) """ | Binary OR It copies a bit, if it exists in either operand. """ print(a | b) """ ^ Binary XOR It copies the bit, if it is set in one operand but not both. """ print(a ^ b) """ ~ Binary Ones Complement It is unary and has the effect of 'flipping' bits. """ print(~a) print(~b) """ << Binary Left Shift The left operand's value is moved left by the number of bits specified by the right operand. """ print(a << b) """ >> Binary Right Shift The left operand's value is moved right by the number of bits specified by the right operand. """ print(a >> b) # Binary operations a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b # 12 = 0000 1100 print("Line 1 - Value of c is ", c) c = a | b # 61 = 0011 1101 print("Line 2 - Value of c is ", c) c = a ^ b # 49 = 0011 0001 print("Line 3 - Value of c is ", c) d = 49 ^ b # 49 = 0011 0001 print("Line 3 - Value of c is ", d) c = ~a # -61 = 1100 0011 print("Line 4 - Value of c is ", c) c = a << 3 # 240 = 1111 0000 print("Line 5 - Value of c is ", c) c = a >> 2 # 15 = 0000 1111 print("Line 6 - Value of c is ", c) # The bit shifts just run off the edge # To determine if a number is even/odd faster than num % 2 num = int(input("Type a number, press enter: ")) if num & 1: print("odd") else: print("even")