Skip navigation

This solution is for the book I am currently reading. All of my notes and solutions are available at Google Code.

Here is my work for problem 2.14 (this one was kind of fun!):

Using only bit-level and logical operations, write a C expression that
is equivalent to x == y. In other words, it will return 1 when x and y
are equal and 0 otherwise.


  bit operations:

  | with 1:   | with 0:
  1 | 1 = 1   1 | 0 = 1
  0 | 1 = 1   0 | 0 = 0

  & with 1:   & with 0:
  1 & 1 = 1   1 & 0 = 0
  0 & 1 = 0   1 & 0 = 0

  ^ with 1:   ^ with 0:
  1 ^ 1 = 0   1 ^ 0 = 1
  0 ^ 1 = 1   0 ^ 0 = 0

  !(x ^ y) should work

different:
  x = 1001001010
  y = 0010010111

      1001001010
   ^  0010010111
  --------------
      1011010101
     !1011010101 = 0x00


equal:
  x = 1000010101
  y = 1000010101

      1000010101
  ^   1000010101
  --------------
      0000000000
     !0000000000 = 0x01

Of course those aren’t bytes, too many bits. Just used some random amount of bits while thinking about it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: