Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

I'm a Mac

macrumors 6502
Original poster
Nov 5, 2007
436
0
Linked lists can be very tricky at first, but once you understand those concepts, they are extremely useful.

The day I realized I could write 2 or more different linked lists but have them point to the same variables in a different order was a BIG "A ha!" moment for me.

What are linked lists? Also, can someone help me with bit operators?
 

deputy_doofy

macrumors 65816
Sep 11, 2002
1,466
410
What are linked lists? Also, can someone help me with bit operators?

Don't worry about them just yet. Once you get to pointers, then linked lists will make sense. If you're just starting out, gets the basics first. I was just recalling an "A ha!" moment for me that helped drive the point(ers) home. :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
as cromulent said, these operators act on the constituent bits of a piece of data. The ones I can think of now are:
^ : xor
& : and
| : or
<< : shift left
>> : shift right

The first 3 act bit by bit the tables are:
xor
Code:
  0  1
0 0  1
1 1  0

And
Code:
  0  1
0 0  0
1 0  1

Or
Code:
  0  1
0 0  1
1 1  1

Xor is the coolest, but that's just a personal opinion.

The shifts act on the whole value. Left will move the left operand the right operand number of bits left, zero filling on the right. Bits shifted off the left side are discarded. Right shift is just the opposite direction.

I will use smaller values that you really can for examples. I will just use a bitstring as the left operator and result to try to make it a little clearer:
0101 << 1 = 1010
0101 << 3 = 1000
0110 >> 2 = 0010
1000 >> 2 = 0010
1110 >> 3 = 0001
1010 >> 1 = 0101

There aren't a lot of things you need to do with these when you're starting out. Eventually they are handy if you are using an int as a bitset to store a bunch of booleans or something, but don't worry too much now.

-Lee
 

laprej

macrumors regular
Oct 14, 2005
108
2
Troy, NY
Xor is the coolest, but that's just a personal opinion.

I always loved the xor swap trick, though it wasn't very clear why it worked until you think about it a little bit...

For example, does this look like it swaps A and B?

A = A ^ B;
B = A ^ B;
A = A ^ B;

I wondered for a while then sat down with a pen and worked it out. Try it! Or, if you're not so curious/inclined, here's the explanation:

A = A ^ B;
// Now the value in A is (A ^ B)
B = A ^ B;
// Remember, A now holds the original values xor'd, so this works out to
// B = (A ^ B) ^ B. xor's are associative, so that is the same as
// writing B = A ^ (B ^ B). Anything xor'd with itself is zero. Anything
// xor'd with zero is itself. So basically, that put the original value of A in B
A = A ^ B;
// A still holds (A ^ B) and B is now A, so this is A = (A ^ B) ^ A which is
// equivalent to A = (A ^ A) ^ B (xor's are also commutative) which again
// uses our observation that anything xor'd with itself is zero and anything
// xor'd with zero is itself, and finally this puts the value of B in A.

Voila. A swap with three of the fastest instructions that your computer can perform. You don't even need a temporary variable. Awesome.
 

I'm a Mac

macrumors 6502
Original poster
Nov 5, 2007
436
0
My head is spinning after reading that... :confused: ;) , but thanks for the description. I'll definitely consult it when I'm ready.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
as interesting as this discussion is, I think it's time to lock this up. If a mod is so inclined the last page or so of posts could be stuck in another thread. The OP's question was answered as best as it might be a while ago.

I agree. Let me conclude with a few links, since other people have explained this in more detail:

The Liskov Substitution Principle (mentions the square-rectangle problem explicitly)

Proper inheritance and substitutability (talks about the circle-ellipse instantiation of the same problem).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.