[OT] help w/ bitwise comparison operators

Kevin D. Clark kclark at CetaceanNetworks.com
Fri Mar 21 09:45:16 EST 2003


Erik Price <eprice at ptc.com> writes:

> Do you really have to convert the number to binary and then do a
> digit-for-digit comparison?  
...
>  Is there something similar for the
> bitwise comparisons for situations like:
> 
> 
> 7 & 3
> 
> or
> 
> 6 | 13

Two silly answers:

Silly solution #1:
------------------

Convert the number to hex.  Do the bitwise comparison a byte at a
time, in your head.  One way to do this would be to construct a 2d
table in your head, like this:

  &     0x01 0x02 ... 0xff
  0x01  0x01 0x00     0x01
  0x02  0x00 0x02     0x02
  ....
  0xff  0x01 0x02     0xff

See, that's not too many table entries to keep in your head.  It might
even be possible for your brain to do this without even maintaining
the table, through some neat property of the human mind.

(but there is an element of seriousness in this suggestion:  notice
that converting the number to hex allows you to operate on the number
a byte at a time, thus limiting the size of the table -- base10
doesn't give you this...)



Silly solution #2
-----------------

Don't bother converting the number to hex.  Instead, construct a
table, similar to the previous one, like this:

  &          1  2  ...
  1          1  0
  2          0  2
  ...

Looking up the answer here is O(1), so this must be a good thing,
right?


My advice:  convert your numbers to binary for the exam, and know that
in the Real World after school, people tend to let the computer do the
work for them.  But it *is* important for you to have this skill.

Regards,

--kevin
-- 
Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA)
cetaceannetworks.com!kclark (GnuPG ID: B280F24E)
alumni.unh.edu!kdc




More information about the gnhlug-discuss mailing list