Logic, Words, and Integers

Logic, Words, and Integers

Computer Science 52 Logic, Words, and Integers 1 Words and Data The basic unit of information in a computer is the bit; it is simply a quantity that takes one of two values, 0 or 1. A sequence of k bits is a k-bit word. Words have no intrinsic meaning beyond the value of the bits. We can assign meaning to words by interpreting them in various ways. Depending on the context we impose, a word can be viewed as an integer, as a boolean value, as a floating point number, as a string of letters, or as an instruction to the computer. A single bit can be viewed as a boolean value with the possible values “false” and “true.” In the next section, we look at the logical operations on truth values—or, equivalently, on single bits. A byte is an 8-bit word. Memory in modern computers is arranged as a sequence of bytes, where adjacent bytes can be considered to be longer words. As we shall see shortly, bytes can be viewed as the binary representations of integers ranging either between 0 and 28 − 1 or between −27 and 27 − 1. 2 Propositional Logic Propositional logic is a language to manipulate truth values. Expressions in propo- sitional logic are constructed from • constants, ? and >; • variables, A, B,...; • parentheses, ) and (; and • connectives, :, ^, _, ), ⊕, and ≡. We interpret the expressions as statements about truth values or bits. The symbol ? is read “bottom” and represents either 0 or “false.” The symbol > is read “top” and represents either 1 or “true.” The connectives represent the following operations: •: means “not” or “(boolean) negation.” 1 A (:A) > ? ? > A B (A ^ B) (A _ B) (A ) B) (A ⊕ B) (A ≡ B) > > > > > ? > > ? ? > ? > ? ? > ? > > > ? ? ? ? ? > ? > Table 1: The truth tables for the propositional connectives. S ! E E ! E ≡ I j I I ! D ) I j D D ! D _ X j X X ! X ⊕ C j C C ! C ^ N j N N !:N j B B ! U j V j (E) U ! ? j > V ! A j B j ::: Table 2: A context-free grammar that generates the expressions of propositional logic. •^ means “and.” •_ means “(inclusive) or.” •) means “implies” or “not greater than.” •⊕ means “exclusive or” or “unequal.” •≡ means “equivalent” or “equal” If we assign truth values to the variables in an expression, we can evaluate the expression. We specify the actions of the connectives using truth tables. Table 1 shows the resulting truth tables for the connectives of propositional logic. The logical expressions may be generated by a context-free grammar, which we dis- cuss elsewhere in the course. For reference, the grammar appears in Table 2. 2 The truth tables for the connectives can be used to evaluate logical expressions. For example, we can verify one of the de Morgan laws, :(A ^ B) = :A _:B: An equality like this one means that the left and right sides have the same truth value, regardless of the truth values of A and B. Table 3 shows some common logical equivalences. We adopt the convention that, in the absence of parentheses, the operation : is done first, followed in order by ^, ⊕, _, ), and ≡. 3 Gates For each connective there is a corresponding component called a gate. A gate takes electrical signals encoding to one or two logical constants as input, and produces an output in the same format. The gates for the fundamental logical operations are listed in Table 4. When working with gates, we usually use 0 and 1 for the logical constants and call them bits. Combinations of logical operations correspond to electrical circuits constructed from gates, as we shall see toward the end of this document. 4 Words As we mentioned, a sequence of k bits is a k-bit word. The operations of propo- sitional logic may be applied to words, in which case they operate on the bits in parallel. For example, the negation of the three-bit word 011 is 100, and the result of an and-operation on 011 and 100 is 000. The study of these operations is called “digital logic.” Words often represent integers. There are two commonly-used ways to interpret words as integers. Unsigned integers represent non-negative values, while signed integers include negative values. 4.1 Unsigned Integers A k-bit word can be interpreted as an unsigned integer by viewing the bits as the “digits” in a binary expansion. Normally we take the right-most bit as the least significant, or the “ones place.” If the word is bk−1bk : : : b1b0, then the value of the unsigned word is k−1 k−1 k−2 1 X i 2 bk−1 + 2 bk−2 + :::+ 2b + b0 = 2 bi: i=0 3 A _ > = > A ^ > = A A _ ? = A A ^ ? = ? A ⊕ > = :A > ) A = A A ⊕ ? = A ?) A = > A ) > = > A ) ? = :A A = ::A (:A ) B) ^ :B = A A = A ^ A A = A _ A A _:A = > A ^ :A = ? A ) A = > A ≡ A = > A ⊕ :A = > A ⊕ A = ? A _ B = B _ A A ^ B = B ^ A A ≡ B = B ≡ A A ) B = :B ):A A ⊕ B = B ⊕ A A _ (B _ C) = (A _ B) _ C A ^ (B ^ C) = (A ^ B) ^ C A ≡ (B ≡ C) = (A ≡ B) ≡ C A ⊕ (B ⊕ C) = (A ⊕ B) ⊕ C A _ (B ^ C) = (A _ B) ^ (A _ C) A ^ (B _ C) = (A ^ B) _ (A ^ C) A _ (A ^ B) = A A ^ (A _ B) = A A ≡ B = (A ) B) ^ (B ) A) A ⊕ B = :(A ≡ B) A ) B = :A _ B A ) B = :(A ^ :B) A _ B = :(:A ^ :B) A ^ B = :(:A _:B) A _ B = :A ) B A ^ B = :(A ):B) Table 3: Some common propositional identities. 4 not xor and nand or nor Table 4: The gates corresponding to the logical connectives. The input lines are on the left, and the output is on the right. Unsigned k-bit integers range between 0 and 2k − 1, inclusive. The operations on unsigned integers are the arithmetic ones: addition, subtraction, multiplication, di- vision, remainder, etc. There are also the usual comparisons: greater, less, greater or equal, and so forth. Often, people make no distinction between a word and the unsigned integer it rep- resents. One use of unsigned integers is to specify locations, or addresses, in a computer’s memory. 4.2 Signed Integers A k-bit word can also be interpreted as a signed integer in the range −2k−1 through 2k−1 −1. The words that have a most significant, or leftmost, bit equal to 1 represent the negative values. The leftmost bit is called the sign bit. The signed value of the word bk−1bk : : : b1b0 is k−2 k−1 k−2 1 k−1 X i −2 bk−1 + 2 bk−2 + :::+ 2b + b0 = −2 bk−1 + 2 bi: i=0 The difference between the unsigned value and the signed value is the minus sign on the largest power of two. Notice that the word “signed” does not mean “negative.” The signed interpretation gives us positive and negative values. This representation of signed integers is called two’s complement. Other representa- tions of signed integers exist, but they are rarely used. The programming languages Java and SML use two’s complement signed integers exclusively. The programming languages C and C++ have both signed and unsigned integer types. Table 5 shows the signed and unsigned values of three-bit words. Most current processors use 32-bit words, although 64-bits is becoming more common. Here, we use fewer bits to make our examples easier, but the ideas are the same. Interestingly, addition and subtraction can be carried out on unsigned and signed interpretations using the same algorithms. We simply use the binary analogs of the 5 integer unsigned signed 7 111 6 110 5 101 4 100 3 011 011 2 010 010 1 001 001 0 000 000 −1 111 −2 110 −3 101 −4 100 Table 5: Three bit words, as unsigned and two’s complement signed values. Ob- serve that the bit string 101 can mean 5 or −3, depending on the interpretation. usual methods for adding and subtracting decimal numbers. Other operations, like comparisons and multiplication, require different methods—depending on whether we are thinking of the values as unsigned or signed. 4.3 Negation and Sign Extension Negation of a two’s complement signed value is accomplished by bitwise comple- menting the value and then adding one (and ignoring any carry into the k + 1-st place). To convince yourself that this process is correct, let x be a k-bit word and x be its bitwise complement. The sum x + x is a word with all 1’s, whose value as a k−1 Pk−2 i signed integer is −2 + i=0 2 = −1. Therefore, x + x = −1, so that x + 1 = −x. We sometimes want to convert a k-bit word into a j-bit word, with j greater than k. If we are thinking of the words as unsigned integers, we just add zeroes on the left. If we are thinking of them as signed integers, then we copy the sign bit on the left as many times as necessary to create the longer integer. The latter process is called sign extension, and it yields a longer (more bits) word representing the same signed integer. For example, −3 is represented in three bits by 101 and in six bits by 111101. 4.4 Words in Modern Computer Systems Computers and programming languages use different size words as their fundamen- tal quantity, and unfortunately, there is considerable variation from one system or 6 language to another.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us