Software CRC - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Software CRC

Description:

The message to be transmitted is viewed as a long positive binary number. ... The devil is in the mundane details: How to handle odd cases? ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 12
Provided by: matthe49
Category:
Tags: crc | mundane | software

less

Transcript and Presenter's Notes

Title: Software CRC


1
Software CRC ICPC I/O
  • Matthew Curry
  • CS497
  • February 6th, 2007

2
Software CRC Problem Statement
  • The message to be transmitted is viewed as a long
    positive binary number. The first byte of the
    message is treated as the most significant byte
    of the binary number. The second byte is the next
    most significant, etc. This binary number will be
    called m'' (for message). Instead of
    transmitting m'' you will transmit a message,
    m2'', consisting of m'' followed by a
    two-byte CRC value.
  • The CRC value is chosen so that m2'' when
    divided by a certain 16-bit value g'' leaves a
    remainder of 0. This makes it easy for the
    receiving program to determine whether the
    message has been corrupted by transmission
    errors. It simply divides any message received by
    g''. If the remainder of the division is zero,
    it is assumed that no error has occurred.
  • You notice that most of the suggested values of
    g'' in the book are odd, but don't see any
    other similarities, so you select the value 34943
    for g'' (the generator value).

3
Techniques Required
  • Method of checking divisibility
  • If (mg) 0, n is divisible by g
  • Arbitrary precision arithmetic!
  • Maximum 8192 bit integers
  • Interpreting a byte string as a number
  • This is a test
  • 84 104 105 115 32
  • 10101000 11010000 11010010 11100110 01000000
  • 1,711,994,770,713,785,234,952,742,657,946,484

4
BigInteger
  • Make an integer from a message
  • byte bytes in.getBytes()BigInteger x new
    BigInteger(bytes)
  • Create space in the message for the checksum
  • x x.shiftLeft(16)
  • Find checksum
  • BigInteger g new BigInteger(34943)g.subtract
    (x.mod(g))

5
Gotchas
  • The devil is in the mundane details How to
    handle odd cases?
  • What if there arent enough characters to fill
    the checksum?
  • String out g.toString(16)while (out.length()
    ! 4) out "0" out
  • Finally Notice the output stops when a is in
    column 1 thisshouldntbeprocessed

6
Conclusion The One-Slider
  • String in sc.nextLine()
  • if (in.charAt(0) )
  • return
  • BigInteger x new BigInteger(in)
  • x x.shiftLeft(16)
  • g new BigInteger("34943")
  • String out ((g.subtract(x.mod(g))).toString(16)
    .toUpperCase()
  • while (out.length() ! 4)
  • out "0" out
  • System.out.println(out.charAt(0) out.charAt(1)
    " " out.charAt(2) out.charAt(3)

7
The ICPC I/O Model
  • I/O can be either stdin or file-based
  • stdin System.in()
  • File-based The input data is in foo.dat
  • Fortunately, we can treat them both the same way!
  • Especially since were using the Scanner most of
    the time

8
Forms of I/O Formatting
  • Read data until some specified condition in the
    input is reached
  • Input number of cases before starting processing
  • Terminate processing with a single zero as last
    input case
  • Only take a single input
  • Read until EOF is reached

9
Strategies for Conditionals
  • Its good style to only have a single exit point
    in a program, loop, etc. Forget about it.
  • while(true) int inputs sc.nextInt() if
    (inputs 0) break
  • Write the skeleton to accept the input before
    filling in the logic
  • For particularly complex input schemes, echo the
    input back to the console

10
Strategies for EOF
  • If each loop is meant to start a new input,
    simply make a while loop with conditionalwhile
    (sc.hasNextX())
  • hasNext has any token available
  • hasNextByte, hasNextInt, hasNextDouble,
    hasNextFloat, hasNextLine, hasNextLong,
    hasNextShort

11
Simulating Judge Input
  • Add test cases to a file, and pipe it into the
    program
  • Open a command prompt (or terminal in UNIX),
    change directories to the appropriate directory,
    and execute the followingjava Main lt
    inputFile.txt
  • This is exactly the method used by judging
    software, and offers no forgiveness to your
    program.
  • Alternatively, deliver an EOF to your program by
    pressing Z on Windows, D on UNIX.
Write a Comment
User Comments (0)
About PowerShow.com