- Forward


Digit Manipulation
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Recall:
    • An int value is "atomic" (i.e., it does not have any constituent parts)
  • The Issue:
    • In many applications we need to manipulate the digits in the int value
  • An Example:
    • Credit cards in which all account numbers are nine digits long and: they never start with a 0, the left-most three digits represent the issuing bank, and the right-most digit indicates the type of account (e.g., debit, credit, crypto currency)
Going Further: Binary Numbers
Back SMYC Forward
  • Recall:
    • int values are represented internally in binary (i.e., base 2)
  • An Implication:
    • Many programming languages allow you to manipulate the individual digits (i.e., bits) of the binary representation
  • Common Bitwise Binary Operators:
    • Shift Left: <<
    • Shift Right: >>
    • Bitwise And: &
    • Bitwise Or: |
Review
Back SMYC Forward

Decimal Representation of an Integer

Base10
Thinking About the Problem
Back SMYC Forward
  • One Idea:
    • The solution will involve the powers of 10
  • Another Idea:
    • The solution will involve arithmetic operators
Thinking About the Problem (cont.)
Back SMYC Forward
  • Addition and/or Multiplication:
    • Just make the number larger
  • Subtraction:
    • Might work but doesn't get you very far (e.g., to get the right-most digit from 7198 you'd need to subtract 7190 which requires knowledge of the three left-most digits)
  • What's Left?
    • Integer Division
    • The Remainder After Integer Division
The Pattern
Back SMYC Forward
  • Drop the Right-Most n Digits:
    • Use integer division (i.e., /)
    • Use a right-side operand of \(10^n\)
  • Extract the Right-Most n Digits:
    • Use the remainder after division (i.e., %)
    • Use a right-side operand of \(10^n\)
The Pattern (cont.)
Back SMYC Forward
  • Extract the Left-Most n Digits:
    • Use integer division (i.e., /)
    • Use a right-side operand of \(10^{N-n}\)
  • Drop the Left-Most n Digits:
    • Use the remainder after division (i.e., %)
    • Use a right-side operand of \(10^{N-n}\)
Examples
Back SMYC Forward
  • The Setting:
    • The account number is 412831758
    • The right-most digit is the card type
    • The issuing bank is the left-most three digits
    • The account holder is identified by the remaining digits
  • Getting the Card Type:
    • \(n = 1\) and \(10^1 = 10\)
    • So the card type is given by: 412831758 % 10
Examples (cont.)
Back SMYC Forward
  • The Setting:
    • The account number is 412831758
    • The right-most digit is the card type
    • The issuing bank is the left-most three digits
    • The account holder is identified by the remaining digits
  • Getting the Bank Number:
    • \(N - n = 9 - 3 = 6\) and \(10^6 = 1000000\)
    • So the bank number is given by: 412831758 / 1000000
Examples (cont.)
Back SMYC Forward
  • The Setting:
    • The account number is 412831758
    • The right-most digit is the card type
    • The issuing bank is the left-most three digits
    • The account holder is identified by the remaining digits
  • Getting the Account Holder:
    • First, drop the left-most three digits (so find the remainder after dividing by \(10^6\))
    • Then, drop the right-most digit (so divide by \(10^1\))
    • So the holder is given by: (412831758 % 1000000) / 10
A Warning
Back SMYC Forward
  • Be Careful:
    • When using this pattern repeatedly, the order matters
  • Example:
    • Try performing the operations for finding the account holder in the other order
There's Always More to Learn
Back -