Hamming
Systems in Rust
Announcements
- Lab Day
- Mixed use assignment.
- Practically useful for HW
- Theoretically useful for Final
Homework
- “Macros” is ready.
- Not too hard; just practice with the operators.
- Due Friday, 26 Sept. at 1440 ET.
Citation
- I learned the term Hamming distance from Prof. Josh Laison while discussing a possible research collaboration.
Today
Hamming
The person
Fine print
- It is a virtual certainty that Hamming did not discover the concepts of Hamming weight or Hamming distance, both of which have limited use in historical record before Hamming was alive.
- Typically I avoid using terms where someone is named after someone who did not invent it.
- I don’t have alternate name in this case.
- It can be called “edit distance” but there are other edit distances.
- It must be remarked upon that an abstract mathematical concept was named after a US Ivy Leaguer who worked on the Manhattan Project, and that this naming is highly political.
Weight
Definition
The Hamming weight of a string is the number of symbols that are different from the zero-symbol of the alphabet used.
- This adopts the formal computer science notion of referring to bit strings as:
- words in the
- languages, which is the set of all possible bitstrings
- that is composed of the symbols, which are the bit values \(\{0, 1\}\)
- So, Hamming weight of a byte is the number of
1
s in that byte.
Your task
- Using the bitwise operators, shifts, and addition, write the following functions:
fn weight_u8(byte:u8) -> u64
fn weight_u64(word:u64) -> u64
fn weight_bytes(bytes:Vec<u8>) -> u64
fn weight_words(words:Vec<u64>) -> u64
- You may wish to follow the Rust convention of implementing these in a
src/lib.rs
file.- You will need to prefix them as
pub fn
insrc/lib.rs
- You will need to prefix them with your package name and
::
insrc/main.rs
- I used
hamming::weight_u8
, for example.
- I used
- You will need to prefix them as
- This convention will be required for the homework, but is not required now.
Template files
- I made a package:
- I wrote a library function:
src/lib.rs
- I wrote some tests:
- After fixing my function, I saw the following:
Distance
Definition
In information theory, the Hamming distance between two strings or vectors of equal length is the number of positions at which the corresponding symbols are different.
- The Hamming weight is the Hamming distance from the string of all
0
symbols.
Your task
- Using the bitwise operators, shifts, and addition, write the following functions:
fn distance_u8(b:u8, c:u8) -> u64
fn distance_u64(w:u64, x:u64) -> u64
fn distance_bytes(bs:Vec<u8>, cs:Vec<u8>) -> u64
fn distance_words(ws:Vec<u64>, xs:Vec<u64>) -> u64
- You may change the parameter names if those don’t make sense to you.
- They were procedurally generated.
Today
Challenge Problems
Strings as bitstrings
- Compute the Hamming distance between the strings
Willamette
andXjmmbnfuuf
. - Successfully pronounce
Xjmmbnfuuf
POPCNT
- Read this paper