9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Russ Cox <russcox@gmail.com>
To: Nigel Roles <nigel@9fs.org>, 9fans <9fans@cse.psu.edu>
Subject: Re: [9fans] OT: puzzle
Date: Mon,  4 Oct 2004 14:45:45 -0400	[thread overview]
Message-ID: <ee9e417a04100411456d4a81e@mail.gmail.com> (raw)
In-Reply-To: <HOEHIDJJJINMLFPOLFJCAEMBDFAA.nigel@9fs.org>

You can do it.  

When I say number-chunk, I mean the 16 bytes you
get for sprint(buf, "X%15d", n).  When I say zero-chunk
I mean 16 NUL bytes.

In the first half of each block, write the fixed pattern and
the block number.

Use the first half to classify the block as management, plain, or xor.

Treat the second half as a sequence of m 16-byte chunks.
Assuming m > 2*log2(n), you can treat each chunk as representing
a single bit in the block number.  When the chunk should represent
a 1 bit, set it to the number-chunk for n.  When the chunk should
represent a 0 bit, set it to the zero-chunk.  Then do the same trick
to encode the complement of the block number.  So if you are encoding
block 10 and you've decided to use 6-bit block numbers, the
second half looks like

  zero-chunk zero-chunk <10>-chunk zero-chunk <10>-chunk zero-chunk
  <10>-chunk <10>-chunk zero-chunk <10>-chunk zero-chunk <10>-chunk

When two number-chunks get xor'ed together, they lose the leading X,
so you know to ignore them.  

When you get an xor'ed block, scan the second half for intact
number-chunks (those that still have the leading X).  You'll find
at least one intact number-chunk from each of the two blocks
that got xor'ed, so just atoi() them to get your answer.

(This works because any two different numbers have to differ in their
binary representation somewhere - you'll get one block's 
pristine number-chunk at that bit position in the normal binary
encoding and the other block's pristine number-chunk at that position
in the complement.)

Russ


      reply	other threads:[~2004-10-04 18:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-04 18:16 Nigel Roles
2004-10-04 18:45 ` Russ Cox [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ee9e417a04100411456d4a81e@mail.gmail.com \
    --to=russcox@gmail.com \
    --cc=9fans@cse.psu.edu \
    --cc=nigel@9fs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).