Calculating Hamming Distance with the IBM Q Experience José Manuel Bravo [email protected] Prof

Total Page:16

File Type:pdf, Size:1020Kb

Calculating Hamming Distance with the IBM Q Experience José Manuel Bravo Pestecnologia@Gmail.Com Prof Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 16 April 2018 doi:10.20944/preprints201804.0164.v2 Calculating Hamming distance with the IBM Q Experience José Manuel Bravo [email protected] Prof. Computer Science and Technology, High School, Málaga, Spain Abstract In this paper a quantum algorithm to calculate the Hamming distance of two binary strings of equal length (or messages in theory information) is presented. The algorithm calculates the Hamming weight of two binary strings in one query of an oracle. To calculate the hamming distance of these two strings we only have to calculate the Hamming weight of the xor operation of both strings. To test the algorithms the quantum computer prototype that IBM has given open access to on the cloud has been used to test the results. Keywords: quantum algorithm, Hamming weight, Hamming distance Introduction This quantum algorithms uses quantum parallelism as the fundamental feature to evaluate The advantage of the quantum computation a function implemented in a quantum oracle for to solve more efficiently than classical many different values of x at the same time computation some algorithms is one of the most because it exploits the ability of a quantum important key for scientists to continue researching computer to be in superposition of different states. and developing news quantum algorithms. Moreover it uses a property of quantum Grover´s searching quantum algorithm is able to mechanics known as interference. In a quantum select one entry between 2푛 with output 1 in only computer it is possible for the two alternatives to O(log(n)) queries to an oracle. Shor´s factoring interfere with one another to yield some global quantum algorithms is able to factorize one property of a function f, by using the Hadamard number faster than any classical algorithms gate to recombine the different alternatives. known. Deutch-Jozsa algorithm is able to The essence of the design of this algorithm is determinate if a function is constant or balanced the choice of a function and a final transformation using only one query to a quantum oracle. that allows efficient determination of the Hamming The Hamming weight of two binary strings weight of a message (information which cannot be (messages) of equal length is the number of attained quickly on a classical computer). symbols different of the zero-symbol used in the The quantum algorithm is based on the alphabet. It represents the number of 1´s in the Deutch-Jozsa algorithm [6]. A special function of string or the digit sum of the binary representation. two binary strings is implemented in an oracle and The Hamming weight is widely used in different we only one query to calculate the Hamming disciplines [1][2], including information theory, weight is needed. coding theory, and cryptography , and his efficient IBM releases in 2016 a 5-qubit universal implementation has been widely studied [3][4][5]. quantum computer prototype accessible on the The Hamming distance in information theory cloud. To test the algorithm, two experiments have between two strings is the minimum number of been simulated on the IBM Q Experience substitutions required to change one string into the composer. other. It is used in coding theory to define the error detecting and the error correcting codes. The The Deutsch-Jozsa algorithm running time of classical algorithms is proportional to the Hamming distance or to the number of bits The goal is to find an algorithm which tells if a in the inputs. Using this quantum algorithm it´s function f is constant or balanced with the least possible to calculate the Hamming distance in only possible number of evaluations of an oracle. one query to a quantum oracle. The algorithm is divided into the following The quantum algorithm calculates the steps: Hamming weight between to binary strings. To 1. Prepare an (n+1)-qubit register in the state ⨂푛 calculate the Hamming distance of these two |Ψ0⟩ = |0⟩ ⨂|1⟩. First n qubits work as inputs we have to calculate previously the xor input qubits of the function, while the (n+1)st operation between them and after that to calculate is an ancilla qubit to store temporary the Hamming weight (hw) of the result. Suppose information. we have to calculate the Hamming distance 2. Apply the Hadamard transformation to the between the binary string A and B (hd (A,B)): register. Then we have the state: A = 1001 B = 1100 2푛−1 1 1 First we calculate A ⊕ B = 0101 and then |Ψ1⟩ = ∑ |푥⟩ ⊗ (|0⟩ − |1⟩) √2푛 √2 hw (A ⊕ B) = hd (A,B) = 2 푥=0 © 2018 by the author(s). Distributed under a Creative Commons CC BY license. Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 16 April 2018 doi:10.20944/preprints201804.0164.v2 3. Perform a quantum transformation Bf, 1 0 0 0 0 0 0 0 associated to a function f defined by 0 0 0 0 1 0 0 0 퐵 |푥⟩|푦⟩ = |푥⟩|푦 ⊕ 푓(푥)⟩ 0 0 0 0 0 1 0 0 f where f(x) is any function 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 푛 푓: {0,1} → {0,1} 0 0 0 0 0 0 1 0 Then we obtain the following state. 0 0 0 1 0 0 0 0 (0 0 0 0 0 0 0 1) 푛 2 −1 1 푓(푥) 1 |Ψ2⟩ = ∑ (−1) |푥⟩ ⊗ (|0⟩ − |1⟩) Then we apply the Bf one time and the √2푛 √2 푥=0 Hadamard transformations. After the n qubits are measured, we can observe: 4. Apply a Hadamard transformation on the first n qubit. ℎ푤(푎) = 2푛−1 ∙ (1 − √푃|0⟩) (1) ℎ푤(푎) = 2푛−1 ∙ √푃|2푛−1⟩ (2) 2푛−1 1 푓(푥)+푥∙푦 1 |Ψ3⟩ = ∑ (−1) |푦⟩ ⊗ (|0⟩ − |1⟩) 푛−1 2푛 √2 where 푃|2 ⟩ is the probability of measure the 푥,푦 state |2푛−1⟩ and 푃|0⟩ is the probability of measure 푥 ∙ 푦 = 푥n-1푦n-1 ⊕ 푥n-2푦n-2 ⊕ … ⊕ 푥0푦0 the state |0⟩ for any number n. As you can see if the Hamming weight tell us the number of symbols 5. The first n qubits are measured. Following different of the zero-symbol used in the alphabet, the algorithm if the probability that the the number 2푛−1 ∙ (1 − 푃|0⟩) tell us the number measurements all give outcome 0, is 1, the √ function f is constant and balanced is that of symbols equal to the zero-symbol in the string. probability is 0. Proof If we want to calculate de Hamming weight of a How to calculate the Hamming weight binary string 푠 of length k (푛 = 푙표푔2(푘) + 1), we define 푚 like the first 2푛−1 bits and 푚 the last 퐿 퐻 푛−1 The algorithm to calculate the Hamming 2 bits of the message: weight is based on the Deutsch-Jozsa algorithm so the steps are the same described above. 푚 = 푚2푛−1 ⋯ 푚0 = 푚퐻 & 푚퐿 The function f associated to the 푚퐻 = 푚2푛−1 ⋯ 푚2푛−1 transformation Bf is the following. Suppose we 푚퐿 = 푚2푛−1−1 … 푚0 = 푠푘 ⋯ 푠1 have to calculate the Hamming weight of a binary string of length k. In that case we define a function Following the algorithm 푚퐻 will be the binary from 푛 = 푙표푔2(푘) + 1 bits to 1 bit in the following string with all zero´s. Now consider the summation way: 푛 On the first 2푛−1 inputs of the function we 2 −1 encode the binary string (message) based on ∑ (−1)푓(푥)+푥∙푦|푦⟩ (3) the position of each bit on the string (message). 푥 On the last 2푛−1 inputs of the function we fix 푛−1 the binary string with all zero´s. for the state |0⟩ and |2 ⟩ in 푚퐻: Suppose we want to calculate the Hamming weight of the string a = 10. Then function f will be: ∑ (−1)0+푥∙(0⋯0)|0⟩ = 2푛−1 (4) 푙표푔2(2)+1 푓: {0,1} → {0,1} 푥∈푚퐻 Input |푥⟩ F(x) 00 0 ∑ (−1)0+푥∙(10⋯0)|2푛−1⟩ = −2푛−1 (5) 01 1 푥∈푚퐻 10 0 11 0 Now if we consider the summation (3) for the state 푛−1 |0⟩ and |2 ⟩ in 푚퐿 we will obtain the same The transformation Bf: result: 퐵f|푥⟩|푦⟩ = |푥⟩|푦 ⊕ 푓(푥)⟩ ∑ (−1)푓(푥)+푥∙(0⋯0)|0⟩ = This transformation has associated a unitary (6) 푥∈푚퐿 permutation so it can be implemented in a quantum 푛−1 = 2 − 2 ∙ ℎ푤(푚퐿) computer using a set of universal quantum gates. Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 16 April 2018 doi:10.20944/preprints201804.0164.v2 qubit is an ancilla qubit to store temporary information. ∑ (−1)푓(푥)+푥∙(10⋯0)|2푛−1⟩ = 4. Apply the steps 2, 3, 4 and 5 of the Deutsch- (7) 푥∈푚퐿 Jozsa algorithm. 푛−1 푛−1 = 2 − 2 ∙ ℎ푤(푚퐿) 5. The states |2 ⟩ and |0⟩ are measured: 푛−1 푛−1 where ℎ푤(푚퐿) is the Hamming weight of the hd (A,B) = hw(C) = 2 ∙ √푃|2 ⟩ substring 푚퐿. After that we consider the hd (A,B) = hw(C) = 2푛−1 ∙ (1 − √푃|0⟩) contribution of (4) and (6) to the amplitude associated with the state|0⟩ and we will obtain Below you can see the probabilities obtained for the Hamming weight (hw) for different n 1 ( 푛−1 ( ) 푛−1) values. 푛 ∙ 2 − 2 ∙ ℎ푤 푚퐿 + 2 = 2 (8) ℎ푤(푚 ) = 1 − 퐿 hw 푷|ퟎ⟩ P|ퟐ풏−ퟏ⟩ 2푛−1 0 1,00 0,00 If we consider the contribution of (5) and (7) 1 0,25 0,25 to the amplitude associated with the state|2푛−1⟩, 2 0,00 1,00 we will obtain Table 1 n=1 1 ∙ (2푛−1 − 2 ∙ ℎ푤(푚 ) − 2푛−1) = 2푛 퐿 hw 푷|ퟎ⟩ P|ퟐ풏−ퟏ⟩ −ℎ푤(푚퐿) (9) 0 1,00000 0,00000 = 푛−1 2 1 0,56250 0,06250 when the state |0⟩ is measured we will obtain: 2 0,25000 0,25000 3 0,06250 0,56250 ℎ푤(푚 ) 2 푃|0⟩ = (1 − 퐿 ) (8) 4 0,00000 1,00000 2푛−1 Table 1 n=2 from where we can easily obtain (1).
Recommended publications
  • Hamming Weight Attacks on Cryptographic Hardware – Breaking Masking Defense?
    Hamming Weight Attacks on Cryptographic Hardware { Breaking Masking Defense? Marcin Gomu lkiewicz1 and Miros law Kuty lowski12 1 Cryptology Centre, Poznan´ University 2 Institute of Mathematics, Wroc law University of Technology, ul. Wybrzeze_ Wyspianskiego´ 27 50-370 Wroc law, Poland Abstract. It is believed that masking is an effective countermeasure against power analysis attacks: before a certain operation involving a key is performed in a cryptographic chip, the input to this operation is com- bined with a random value. This has to prevent leaking information since the input to the operation is random. We show that this belief might be wrong. We present a Hamming weight attack on an addition operation. It works with random inputs to the addition circuit, hence masking even helps in the case when we cannot control the plaintext. It can be applied to any round of the encryption. Even with moderate accuracy of measuring power consumption it de- termines explicitly subkey bits. The attack combines the classical power analysis (over Hamming weight) with the strategy of the saturation at- tack performed using a random sample. We conclude that implementing addition in cryptographic devices must be done very carefully as it might leak secret keys used for encryption. In particular, the simple key schedule of certain algorithms (such as IDEA and Twofish) combined with the usage of addition might be a serious danger. Keywords: cryptographic hardware, side channel cryptanalysis, Ham- ming weight, power analysis 1 Introduction Symmetric encryption algorithms are often used to protect data stored in inse- cure locations such as hard disks, archive copies, and so on.
    [Show full text]
  • Type I Codes Over GF(4)
    Type I Codes over GF(4) Hyun Kwang Kim¤ San 31, Hyoja Dong Department of Mathematics Pohang University of Science and Technology Pohang, 790-784, Korea e-mail: [email protected] Dae Kyu Kim School of Electronics & Information Engineering Chonbuk National University Chonju, Chonbuk 561-756, Korea e-mail: [email protected] Jon-Lark Kimy Department of Mathematics University of Louisville Louisville, KY 40292, USA e-mail: [email protected] Abstract It was shown by Gaborit el al. [10] that a Euclidean self-dual code over GF (4) with the property that there is a codeword whose Lee weight ´ 2 (mod 4) is of interest because of its connection to a binary singly-even self-dual code. Such a self-dual code over GF (4) is called Type I. The purpose of this paper is to classify all Type I codes of lengths up to 10 and extremal Type I codes of length 12, and to construct many new extremal Type I codes over GF (4) of ¤The present study was supported by Com2MaC-KOSEF, POSTECH BSRI research fund, and grant No. R01-2006-000-11176-0 from the Basic Research Program of the Korea Science & Engineering Foundation. ycorresponding author, supported in part by a Project Completion Grant from the University of Louisville. 1 lengths from 14 to 22 and 34. As a byproduct, we construct a new extremal singly-even self-dual binary [36; 18; 8] code, and a new ex- tremal singly-even self-dual binary [68; 34; 12] code with a previously unknown weight enumerator W2 for ¯ = 95 and γ = 1.
    [Show full text]
  • Arxiv:1311.6244V1 [Physics.Comp-Ph] 25 Nov 2013
    An efficient implementation of Slater-Condon rules Anthony Scemama,∗ Emmanuel Giner November 26, 2013 Abstract Slater-Condon rules are at the heart of any quantum chemistry method as they allow to simplify 3N- dimensional integrals as sums of 3- or 6-dimensional integrals. In this paper, we propose an efficient implementation of those rules in order to identify very rapidly which integrals are involved in a matrix ele- ment expressed in the determinant basis set. This implementation takes advantage of the bit manipulation instructions on x86 architectures that were introduced in 2008 with the SSE4.2 instruction set. Finding which spin-orbitals are involved in the calculation of a matrix element doesn't depend on the number of electrons of the system. In this work we consider wave functions Ψ ex- For two determinants which differ by two spin- pressed as linear combinations of Slater determinants orbitals: D of orthonormal spin-orbitals φ(r): jl hDjO1jDiki = 0 (4) X Ψ = c D (1) jl i i hDjO2jDiki = hφiφkjO2jφjφli − hφiφkjO2jφlφji i All other matrix elements involving determinants Using the Slater-Condon rules,[1, 2] the matrix ele- with more than two substitutions are zero. ments of any one-body (O1) or two-body (O2) oper- An efficient implementation of those rules requires: ator expressed in the determinant space have simple expressions involving one- and two-electron integrals 1. to find the number of spin-orbital substitutions in the spin-orbital space. The diagonal elements are between two determinants given by: 2. to find which spin-orbitals are involved in the X substitution hDjO1jDi = hφijO1jφii (2) i2D 3.
    [Show full text]
  • Faster Population Counts Using AVX2 Instructions
    Faster Population Counts Using AVX2 Instructions Wojciech Mula,Nathan Kurz and Daniel Lemire∗ ?Universit´edu Qu´ebec (TELUQ), Canada Email: [email protected] Counting the number of ones in a binary stream is a common operation in database, information-retrieval, cryptographic and machine-learning applications. Most processors have dedicated instructions to count the number of ones in a word (e.g., popcnt on x64 processors). Maybe surprisingly, we show that a vectorized approach using SIMD instructions can be twice as fast as using the dedicated instructions on recent Intel processors. The benefits can be even greater for applications such as similarity measures (e.g., the Jaccard index) that require additional Boolean operations. Our approach has been adopted by LLVM: it is used by its popular C compiler (Clang). Keywords: Software Performance; SIMD Instructions; Vectorization; Bitset; Jaccard Index 1. INTRODUCTION phy [5], e.g., as part of randomness tests [6] or to generate pseudo-random permutations [7]. They can help find We can represent all sets of integers in f0; 1;:::; 63g duplicated web pages [8]. They are frequently used in using a single 64-bit word. For example, the word 0xAA bioinformatics [9, 10, 11], ecology [12], chemistry [13], (0b10101010) represents the set f1; 3; 5; 7g. Intersections and so forth. Gueron and Krasnov use population-count and unions between such sets can be computed using instructions as part of a fast sorting algorithm [14]. a single bitwise logical operation on each pair of words The computation of the population count is so (AND, OR). We can generalize this idea to sets of important that commodity processors have dedicated integers in f0; 1; : : : ; n − 1g using dn=64e 64-bit words.
    [Show full text]
  • Coding Theory and Applications Solved Exercises and Problems Of
    Coding Theory and Applications Solved Exercises and Problems of Linear Codes Enes Pasalic University of Primorska Koper, 2013 Contents 1 Preface 3 2 Problems 4 2 1 Preface This is a collection of solved exercises and problems of linear codes for students who have a working knowledge of coding theory. Its aim is to achieve a balance among the computational skills, theory, and applications of cyclic codes, while keeping the level suitable for beginning students. The contents are arranged to permit enough flexibility to allow the presentation of a traditional introduction to the subject, or to allow a more applied course. Enes Pasalic [email protected] 3 2 Problems In these exercises we consider some basic concepts of coding theory, that is we introduce the redundancy in the information bits and consider the possible improvements in terms of improved error probability of correct decoding. 1. (Error probability): Consider a code of length six n = 6 defined as, (a1; a2; a3; a2 + a3; a1 + a3; a1 + a2) where ai 2 f0; 1g. Here a1; a2; a3 are information bits and the remaining bits are redundancy (parity) bits. Compute the probability that the decoder makes an incorrect decision if the bit error probability is p = 0:001. The decoder computes the following entities b1 + b3 + b4 = s1 b1 + b3 + b5 == s2 b1 + b2 + b6 = s3 where b = (b1; b2; : : : ; b6) is a received vector. We represent the error vector as e = (e1; e2; : : : ; e6) and clearly if a vector (a1; a2; a3; a2+ a3; a1 + a3; a1 + a2) was transmitted then b = a + e, where '+' denotes bitwise mod 2 addition.
    [Show full text]
  • Conversion of Mersenne Twister to Double-Precision Floating-Point
    Conversion of Mersenne Twister to double-precision floating-point numbers Shin Harasea,∗ aCollege of Science and Engineering, Ritsumeikan University, 1-1-1 Nojihigashi, Kusatsu, Shiga, 525-8577, Japan. Abstract The 32-bit Mersenne Twister generator MT19937 is a widely used random number generator. To generate numbers with more than 32 bits in bit length, and particularly when converting into 53-bit double-precision floating-point numbers in [0, 1) in the IEEE 754 format, the typical implementation con- catenates two successive 32-bit integers and divides them by a power of 2. In this case, the 32-bit MT19937 is optimized in terms of its equidistribution properties (the so-called dimension of equidistribution with v-bit accuracy) under the assumption that one will mainly be using 32-bit output values, and hence the concatenation sometimes degrades the dimension of equidis- tribution compared with the simple use of 32-bit outputs. In this paper, we analyze such phenomena by investigating hidden F2-linear relations among the bits of high-dimensional outputs. Accordingly, we report that MT19937 with a specific lag set fails several statistical tests, such as the overlapping collision test, matrix rank test, and Hamming independence test. Keywords: Random number generation, Mersenne Twister, Equidistribution, Statistical test 2010 MSC: arXiv:1708.06018v4 [math.NA] 2 Sep 2018 65C10, 11K45 1. Introduction Random number generators (RNGs) (or more precisely, pseudorandom number generators) are an essential tool for scientific computing, especially ∗Corresponding author Email address: [email protected] (Shin Harase) Preprint submitted to Elsevier September 5, 2018 for Monte Carlo methods.
    [Show full text]
  • Applications of Search Techniques to Cryptanalysis and the Construction of Cipher Components. James David Mclaughlin Submitted F
    Applications of search techniques to cryptanalysis and the construction of cipher components. James David McLaughlin Submitted for the degree of Doctor of Philosophy (PhD) University of York Department of Computer Science September 2012 2 Abstract In this dissertation, we investigate the ways in which search techniques, and in particular metaheuristic search techniques, can be used in cryptology. We address the design of simple cryptographic components (Boolean functions), before moving on to more complex entities (S-boxes). The emphasis then shifts from the construction of cryptographic arte- facts to the related area of cryptanalysis, in which we first derive non-linear approximations to S-boxes more powerful than the existing linear approximations, and then exploit these in cryptanalytic attacks against the ciphers DES and Serpent. Contents 1 Introduction. 11 1.1 The Structure of this Thesis . 12 2 A brief history of cryptography and cryptanalysis. 14 3 Literature review 20 3.1 Information on various types of block cipher, and a brief description of the Data Encryption Standard. 20 3.1.1 Feistel ciphers . 21 3.1.2 Other types of block cipher . 23 3.1.3 Confusion and diffusion . 24 3.2 Linear cryptanalysis. 26 3.2.1 The attack. 27 3.3 Differential cryptanalysis. 35 3.3.1 The attack. 39 3.3.2 Variants of the differential cryptanalytic attack . 44 3.4 Stream ciphers based on linear feedback shift registers . 48 3.5 A brief introduction to metaheuristics . 52 3.5.1 Hill-climbing . 55 3.5.2 Simulated annealing . 57 3.5.3 Memetic algorithms . 58 3.5.4 Ant algorithms .
    [Show full text]
  • Calculating Hamming Distance with the IBM Q Experience José Manuel Bravo [email protected] Prof
    Preprints (www.preprints.org) | NOT PEER-REVIEWED | Posted: 12 April 2018 doi:10.20944/preprints201804.0164.v1 Calculating Hamming distance with the IBM Q Experience José Manuel Bravo [email protected] Prof. Computer Science and Technology, High School, Málaga, Spain Abstract In this brief paper a quantum algorithm to calculate the Hamming distance of two binary strings of equal length (or messages in theory information) is presented. The algorithm calculates the Hamming weight of two binary strings in one query of an oracle. To calculate the hamming distance of these two strings we only have to calculate the Hamming weight of the xor operation of both strings. To test the algorithms the quantum computer prototype that IBM has given open access to on the cloud has been used to test the results. Keywords: quantum algorithm, Hamming weight, Hamming distance Introduction This quantum algorithms uses quantum parallelism as the fundamental feature to evaluate The advantage of the quantum computation a function implemented in a quantum oracle for to solve more efficiently than classical many different values of x at the same time computation some algorithms is one of the most because it exploits the ability of a quantum important key for scientists to continue researching computer to be in superposition of different states. and developing news quantum algorithms. Moreover it uses a property of quantum Grover´s searching quantum algorithm is able to mechanics known as interference. In a quantum select one entry between 2 with output 1 in only computer it is possible for the two alternatives to O(log(n)) queries to an oracle.
    [Show full text]
  • FED-STD-1037C the Number of Digit Positions in Which the Corresponding Digits of Two Binary Numbers Or Words of the Same Length
    FED-STD-1037C Hagelbarger code: A Hamming distance between 1011101 and 1001001 convolutional code that enables is two. Note 2: The concept can be extended to error bursts to be corrected other notation systems. For example, the Hamming provided that there are relatively distance between 2143896 and 2233796 is three, long error-free intervals between and between “toned” and “roses” it is also three. the error bursts. Note: In the Synonym signal distance. Hagelbarger code, inserted parity check bits are spread out in time so that an error burst is not likely to affect more than one of the groups in which parity is checked. half-duplex (HDX) operation: Operation in which communication between two terminals occurs in either direction, but in only one direction at a time. (188) Note: Half-duplex operation may occur on a half-duplex circuit or on a duplex circuit, but it may not occur on a simplex circuit. Synonyms one-way reversible operation, two-way alternate operation. halftone: Any photomechanical printing surface or the impression therefrom in which detail and tone values are represented by a series of evenly spaced dots in varying size and shape, varying in direct the number of digit positions in which the proportion to the intensity of tones they represent. corresponding digits of two binary numbers or [JP1] words of the same length are different halftone characteristic: 1. In facsimile systems, the Hamming weight: The number of non-zero symbols relationship between the density of the recorded in a symbol sequence. Note: For binary signaling, copy and the density of the object, i.e., the original.
    [Show full text]
  • Bits and Bit Sequences Integers
    Data ● (Some repeating CS1083, ECE course) ● bits and bit sequences ● integers (signed and unsigned) ● bit vectors ● strings and characters ● floating point numbers ● hexadecimal and octal notations Bits and Bit Sequences ● Fundamentally, we have the binary digit, 0 or 1. ● More interesting forms of data can be encoded into a bit sequence. ● 00100 = “drop the secret package by the park entrance” 00111 = “Keel Meester Bond” ● A given bit sequence has no meaning unless you know how it has been encoded. ● Common things to encode: integers, doubles, chars. And machine instructions. Encoding things in bit sequences (From textbook) ● Floats ● Machine Instructions How Many Bit Patterns? ● With k bits, you can have 2k different patterns ● 00..00, 00..01, 00..10, … , 11..10, 11..11 ● Remember this! It explains much... ● E.g., if you represent numbers with 8 bits, you can represent only 256 different numbers. Names for Groups of Bits ● nibble or nybble: 4 bits ● octet: 8 bits, always. Seems pedantic. ● byte: 8 bits except with some legacy systems. In this course, byte == octet. ● after that, it gets fuzzy (platform dependent). For 32-bit ARM, – halfword: 16 bits – word: 32 bits Unsigned Binary (review) ● We can encode non-negative integers in unsigned binary. (base 2) ● 10110 = 1*24 + 0*23 + 1*22 + 1*21 +1*20 represents the mathematical concept of “twenty-two”. In decimal, this same concept is written as 22 = 2*101 + 2*100. ● Converting binary to decimal is just a matter of adding up powers of 2, and writing the result in decimal. ● Going from decimal to binary is trickier.
    [Show full text]
  • A Loopless Gray Code for Minimal Signed-Binary Representations
    A Loopless Gray Code for Minimal Signed-Binary Representations Gurmeet Singh Manku1 and Joe Sawada2 1 Google Inc., USA [email protected] http://www.cs.stanford.edu/∼manku 2 University of Guelph, Canada [email protected] http://www.cis.uoguelph.ca/∼sawada Abstract. Astring...a2a1a0 over the alphabet {−1, 0, 1} is said to be k a minimal signed-binary representation of an integer n if n = k≥0 ak2 and the number of non-zero digits is minimal. We present a loopless (and hence a Gray code) algorithm for generating all minimal signed binary representations of a given integer n. 1 Introduction Astring...a a a is said to be a signed-binary representation 2 1 0 ¯¯ ¯ n n a k a ∈{− , , } 1101001101 (SBR) of an integer if = k≥0 k2 and k 1 0 1 ¯ ¯¯ ¯ k 10101001101 for all .Aminimal SBR has the least number of non-zero dig- 101¯100¯ 1¯10¯ 1¯ ¯ its. For example, 45 has five minimal SBRs: 101101, 110101, 101¯10¯ 1010¯ 1¯ 1010¯ 101,¯ 10100¯ 1¯1¯ and 11001¯1,¯ where 1¯ denotes −1. Our main 101010¯ 1010¯ 1¯ result is a loopless algorithm that generates all minimal SBRs 110101010¯ 1¯ for an integer n in Gray code order. See Fig. 1 for an exam- 1100110101¯ ple. Our algorithm requires linear time for generating the first 1010011010¯ 1¯ string. Thereafter, only O(1)timeisrequiredintheworst-case 10100110011¯ for identifying the portion of the current string to be modified 1100110011 ¯ for generating the next string1. 1101010011 101010¯ 10011¯ Volumes 3 and 4 of Knuth’s The Art of Computer Pro- 101¯10¯ 10011¯ gramming are devoted entirely to algorithms for generation of combinatorial objects.
    [Show full text]
  • Arxiv:1809.02161V1 [Cs.PL] 6 Sep 2018 – Little-Used and Unclear Corner Cases in the Language Standards
    Future Directions for Optimizing Compilers Nuno P. Lopes1 and John Regehr2 1 Microsoft Research, UK [email protected] 2 University of Utah, USA [email protected] September , Introduction As software becomes larger, programming languages become higher-level, and processors continue to fail to be clocked faster, we’ll increasingly require compilers to reduce code bloat, eliminate abstraction penalties, and exploit interesting instruction sets. At the same time, compiler execution time must not increase too much and also compilers should never produce the wrong output. This paper examines the problem of making optimizing compilers faster, less buggy, and more capable of generating high-quality output. Why are Compilers Slow? While very fast compilers exist, heavily optimizing ahead-of-time compilers are generally not fast. First, many of the sub-problems that compilers are trying to solve, such as optimal instruction selection, are themselves intractable. Second, after performing basic optimizations that are always a good idea, and that usually reduce code size, a compiler is confronted with optimization opportunities (auto- vectorization, loop unrolling, function inlining, etc.) that have a less clear payoff and that often increase the amount of code that has to be subsequently processed. Third, the compiler would like to reach a fixed point where every obviously desirable transformation has been performed; there is rarely enough time to do this. Finally, fully breaking down the abstractions in high-level-language code is not an easy job. Why are Compilers Wrong? Compiler bugs have diverse root causes: – Difficult-to-avoid pitfalls in unsafe compiler implementation languages— commonly, null pointer dereferences and use-after-free errors.
    [Show full text]