-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits.py
More file actions
150 lines (126 loc) · 3.74 KB
/
bits.py
File metadata and controls
150 lines (126 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
################################################################################
#
# Description:
#
# This module implements various bit manipulation functions.
# The main purpose of the module is to help students of The University
# of Melbourne subject COMP10001 "Foundations of Computing" with their
# project for steganography. When working with characters we assume they
# are within the ASCII set, and will be encoded in exactly 8 bits, with
# zeros padding on the left where necessary.
#
# Authors:
#
# Bernie Pope (bjpope@unimelb.edu.au)
#
# Date created:
#
# 1 August 2013
#
# Date modified and reason:
#
# 10 Aug 2013: Generalised functions for setting bits to any bit in
# an integer, not just the LSB.
# 13 Sep 2013: Update code for COMP10001 project 2, semester 2 2013.
# Added comments.
#
################################################################################
def char_to_bits(char):
"""char_to_bits(char) -> string
Convert the input ASCII character to an 8 bit string of 1s and 0s.
>>> char_to_bits('A')
'01000001'
"""
result = ''
char_num = ord(char)
for index in range(8):
result = get_bit(char_num, index) + result
return result
def bits_to_char(bits):
"""bits_to_char(bit_string) -> char
Convert a string of bits (1s and 0s) into its corresponding integer value,
and then convert that integer value to its corresponding ASCII character.
The input bit_string should be non-empty, and must contain only the characters
1 and 0.
>>> bits_to_char('01000001')
'A'
"""
return chr(int(bits, 2))
def get_bit(int, position):
"""get_bit(int, position) -> bit
Return the bit (as a character, '1' or '0') from a given position
in a given integer (interpreted in base 2).
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> for pos in range(8):
... print(b.get_bit(167, pos))
...
1
1
1
0
0
1
0
1
"""
if int & (1 << position):
return '1'
else:
return '0'
def set_bit_on(int, position):
"""set_bit_on(int, position) -> int
Set the bit at a given position in a given integer to 1,
regardless of its previous value, and return the new integer
as the result.
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> set_bit_on(0, 0)
1
>>> set_bit_on(0, 1)
2
>>> set_bit_on(167, 3)
175
"""
return int | (1 << position)
def set_bit_off(int, position):
"""set_bit_off(int, position) -> int
Set the bit at a given position in a given integer to 0,
regardless of its previous value, and return the new integer
as the result.
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> set_bit_off(0, 0)
0
>>> set_bit_off(1, 0)
0
>>> set_bit_off(167, 0)
166
>>> set_bit_off(175, 3)
167
"""
return int & ~(1 << position)
def set_bit(int, bit, position):
"""set_bit(int, bit, position) -> int
Set the bit at a given position to the given bit (as a char, either
'1' or '0') regardless of its previous value, and return the new integer
as the result.
The least significant bit is at position 0. The second-least significant
bit is at position 1, and so forth.
>>> set_bit(0, '1', 0)
1
>>> set_bit(0, '1', 1)
2
>>> set_bit(0, '1', 2)
4
>>> set_bit(0, '1', 3)
8
>>> set_bit(175, '0', 3)
167
>>> set_bit(175, '1', 3)
175
"""
if bit == '1':
return set_bit_on(int, position)
else:
return set_bit_off(int, position)