B
This commit is contained in:
parent
e5e8880e41
commit
518da4a923
1019 changed files with 15877 additions and 0 deletions
51
Task/Bitwise-IO/Python/bitwise-io-1.py
Normal file
51
Task/Bitwise-IO/Python/bitwise-io-1.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
class BitWriter:
|
||||
def __init__(self, f):
|
||||
self.accumulator = 0
|
||||
self.bcount = 0
|
||||
self.out = f
|
||||
|
||||
def __del__(self):
|
||||
self.flush()
|
||||
|
||||
def writebit(self, bit):
|
||||
if self.bcount == 8 :
|
||||
self.flush()
|
||||
if bit > 0:
|
||||
self.accumulator |= (1 << (7-self.bcount))
|
||||
self.bcount += 1
|
||||
|
||||
def writebits(self, bits, n):
|
||||
while n > 0:
|
||||
self.writebit( bits & (1 << (n-1)) )
|
||||
n -= 1
|
||||
|
||||
def flush(self):
|
||||
self.out.write(chr(self.accumulator))
|
||||
self.accumulator = 0
|
||||
self.bcount = 0
|
||||
|
||||
|
||||
class BitReader:
|
||||
def __init__(self, f):
|
||||
self.input = f
|
||||
self.accumulator = 0
|
||||
self.bcount = 0
|
||||
self.read = 0
|
||||
|
||||
def readbit(self):
|
||||
if self.bcount == 0 :
|
||||
a = self.input.read(1)
|
||||
if ( len(a) > 0 ):
|
||||
self.accumulator = ord(a)
|
||||
self.bcount = 8
|
||||
self.read = len(a)
|
||||
rv = ( self.accumulator & ( 1 << (self.bcount-1) ) ) >> (self.bcount-1)
|
||||
self.bcount -= 1
|
||||
return rv
|
||||
|
||||
def readbits(self, n):
|
||||
v = 0
|
||||
while n > 0:
|
||||
v = (v << 1) | self.readbit()
|
||||
n -= 1
|
||||
return v
|
||||
9
Task/Bitwise-IO/Python/bitwise-io-2.py
Normal file
9
Task/Bitwise-IO/Python/bitwise-io-2.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#! /usr/bin/env python
|
||||
import sys
|
||||
import bitio
|
||||
|
||||
o = bitio.BitWriter(sys.stdout)
|
||||
c = sys.stdin.read(1)
|
||||
while len(c) > 0:
|
||||
o.writebits(ord(c), 7)
|
||||
c = sys.stdin.read(1)
|
||||
10
Task/Bitwise-IO/Python/bitwise-io-3.py
Normal file
10
Task/Bitwise-IO/Python/bitwise-io-3.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#! /usr/bin/env python
|
||||
import sys
|
||||
import bitio
|
||||
|
||||
r = bitio.BitReader(sys.stdin)
|
||||
while True:
|
||||
x = r.readbits(7)
|
||||
if ( r.read == 0 ):
|
||||
break
|
||||
sys.stdout.write(chr(x))
|
||||
Loading…
Add table
Add a link
Reference in a new issue