[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference noted::hackers_v1

Title:-={ H A C K E R S }=-
Notice:Write locked - see NOTED::HACKERS
Moderator:DIEHRD::MORRIS
Created:Thu Feb 20 1986
Last Modified:Mon Aug 03 1992
Last Successful Update:Fri Jun 06 1997
Number of topics:680
Total number of notes:5456

456.0. "Programming spiffy" by YALI::LASTOVICA (Stuck in a Lather-Rinse-Repeat loop) Tue Apr 28 1987 00:20

    I'm sure that all the *real* programmers out there know this one,
    but I just learned about it...  What does it do?  You'll have to
    try and find out.  I think it's real clever:
    
    	XOR	R1,R0
    	XOR	R0,R1
    	XOR	R1,R0
T.RTitleUserPersonal
Name
DateLines
456.1TEMP=a; a=b; b=TEMP;USHS01::BLANDOTue Apr 28 1987 01:005
    It swaps R1 and R0!  I learned that in CS 101...  I am not trying
    to bragg, or put down anyone.  As a side, I have known that for 90%
    of my career, and I am yet to use it!
    
    FJBlando
456.2CHOVAX::YOUNGBack from the Shadows Again,Tue Apr 28 1987 03:564
    Yes, in most architectures it is slower, but has the single advantage
    that it does not require an extra storage space.
    
    --  Barry
456.3CAFEIN::PFAUNow where did I leave my marbles?Tue Apr 28 1987 13:458
    Another method of swapping values without using an extra storage
    location:
    
    	a = a + b
    	b = a - b
    	a = a - b
    
    tom_p
456.4IBM's XC instructionWHICH::EVANSRobert N. Evans DTN-225-6946 HLO2-3/P4Tue Apr 28 1987 13:4824
I first heard of this hack used on an IBM 360.  Their XOR instruction is a
character string instruction.  Thus one could use this hack to easily exchange
the contents of two buffers.  In the days when 256Kb was a large memory, and
all of your program had to fit into memory, (along with OS and HASP), saving the
need for a 256 byte buffer might have been useful.

I have never actually seen this hack used in any code.

Another way to use this trick is to design a logic circuit without any
crossed wires that routes the signal A to A' and B to B':

                                                +-----+
--------> A ---+------------------------------->|     |
               |                                | XOR |------> B' ------------->
               |  +-----+           +---------->|     |
               +->|     |           |           +-----+
                  | XOR |-----------+
               +->|     |           |           +-----+
               |  +-----+           +---------->|     |
               |                                | XOR |------> A'------------->
--------> B ---+------------------------------->|     |
                                                +-----+

(Equally interesting and useless)
456.5Read Planiverse.MOSAIC::WASSERJohn A. WasserTue Apr 28 1987 18:157
> design a logic circuit without any crossed wires that routes the 
> signal A to A' and B to B':
>(Equally interesting and useless)

	Not to a flatlander.  In a 2 dimensional universe, it's the
	only way to cross wires!
			-John A. Wasser
456.6make sure cells are different, logic problem24799::OSMANtype video::user$7:[osman]eric.sixTue Apr 28 1987 20:2218
re: your tricks for swapping two cells without a third:


	Be careful that the two cells aren't ever the same cell.
	If so, the methods don't work !


re:  logic circuits, a harder one


	Design a circuit that uses and's, or's, and up to 2 not's, that
	takes boolean signals A, B, and C as inputs, and produces boolean
	signals -A, -B, and -C as outputs.

	In order to get credit for this one, you have to draw it like
	that previous reply drew the swap circuit.

/Eric
456.7ERIS::CALLASSo many ratholes, so little timeWed Apr 29 1987 20:005
    re .5:
    
    And no doubt they use it extensively!
    
    	Jon
456.8VIDEO::LEICHTERJJerry LeichterMon May 04 1987 01:0312
Challenge:  You are given a one-way linked list; you need to be able to
traverse it both forward and backward.  Implement this so that (a) you
need a fixed amount of extra memory, independent of the number of items
in the list; (b) the amount of time to move either forward or backward
by one step is independent of the number of items in the list; (c) there
can be any number of independent pieces of code traversing the list,
in either direction, at the same time.  (In fact, they can be running
truely in parallel.  Note that this access is for READ - even for the
one-way list, you need to do something else to allow for writes.)

Hint:  .0.
							-- Jerry
456.9?CHOVAX::YOUNGBack from the Shadows Again,Mon May 04 1987 02:196
    Re .8:
    
    Unless you've left something out, a fixed array of fixed-size elements
    meets this requirement.
    
    --  Barry
456.10UFP::MURPHYEuropean or African Swallow?Mon May 04 1987 11:326
    Re: .9:
    No, that won't work.. you would need to allocate a massive "fixed
    size" array; since this is a linked list, I presume that elements
    can be allocated and deallocated "on the fly".
    (I'm still working on a solution..)
    	-Rick
456.11What's an extra longword among friends?ERIS::CALLASSo many ratholes, so little timeMon May 04 1987 12:507
    re .8:
    
    Here's the Gordian Knot solution:
    
    Rewrite the program so that it uses a two-way linked list.
    
    		Jon
456.12Recalled from 8 years ago...TLE::BRETTMon May 04 1987 13:2824
    Solution follows...
    
    
    Each "link" stores the offset from the previous element to the next
    element, and you keep two pointers, one to the current node and
    one to either the next or the previous.
    
    ie: Using the Ada syntax FOO'address to mean the address of FOO...
    
    		A(I-1).LINK 	:= 	A(I)'address   - A(I-2)'address;
    		A(I).LINK	:=	A(I+1)'address - A(I-1)'address;
    		A(I+1).LINK	:=	A(I+2)'address - A(I)'address;
                                     
    
    Position is stored as A(I-1)'address and A(I)'address.
    To move forward
    
    		A(I+1)'address = A(I-1)'address + A(I).LINK 
    
    To move backward
    
    		A(I-2)'address = A(I)'address - A(I-1).LINK
    
    /Bevin
456.13XOR does the trickTLE::RMEYERSRandy MeyersMon May 04 1987 21:1616
The think that the solution wanted is another creative use of XOR.

Each node in the linked list only has storage for one pointer.  In
that pointer slot, store the XOR of the address of the previous element
in the list with the address of the next element of the list.

To walk forward in the list, you XOR the contents of the pointer word
of the current element with the address of the previous element to give
the pointer to the next element.  To walk backwards in the list, you XOR
the pointer word in the current element with the address of the next
element in the list to yield the address of the previous element.

Thus, the use of one pointer variable (used to keep track of where you
were) saves you one pointer member in all your list nodes.

I have never had need to use this trick, but I do think it is cute.
456.14.12 and .13 equivalentPEANO::GLASERSteve Glaser DTN 226-7646 LKG1-2/A19Wed May 06 1987 00:1216
    .12 and .13 are equivalent but the xor won't generate integer
    overflows.
    
    
    Extra credit:  How can you do this atomically?

    Answer: 
    
    You can't on a Vax, but the IBM 370 has this nifty instruction "Compare
    Double and Swap" that reads 2 longwords, compares them against
    specified values and, if they match, writes new valued back into the
    longwords.  The instruction is atomic, even in the case of
    multiprocessors.  There's also a "compare and swap" for singly linked
    lists. 
    
    Steveg
456.15VIDEO::LEICHTERJJerry LeichterTue May 12 1987 02:0214
.13 was what I had in mind; .12 will also work (I've never worked out the
details, but in principle you need not lose any information on overflow -
2's complement arithmetic is just arithmetic mod 2^n, which is a well-behaved
goup; but you have to be careful).

The fact that this seemd to give you "something for nothing" bothered me for
a while, until I thought of the trick mentioned above of reversing the pointers
as you move along.  This trick is basic to good garbage-collection algorithms.
The neat thing about the XOR hack is that it doesn't change the list.

A nice way to look at what is going on here is to imagine that rather than
holding on to a "current NODE" pointer, you are holding on to a "current
EDGE" pointer.
							-- Jerry