Serial data manipulation AS5045 [Solved]

My AS5045 Magnetic rotation sensor, as yet unconnected, will provide an 18 bit serial data stream, the first 12 bits give a rotational direction, 0 to 360 degrees, followed by 6 status bits. I don't know how to manipulate binary numbers.
I obtained this code from a kind American but I don't think it is complete. "packeddata" and "angle" appear in the code but do not seem to be filled. It seems to me that there are missing lines which would express the angle. Can anyone suggest what should be inserted please? The code would not verify and I added a } to make it verify so evidently it had been corrupted. I note delays are commented incorrectly.

//AS5045 Magnetometer code using serial data.

const int ledPin = 13; // LED connected to digital pin 13, used as a heartbeat
const int clockPin = 7; // output to clock
const int CSnPin = 6; // output to chip select
const int inputPin = 2; // read AS5045

int inputstream = 0; // one bit read from pin
long packeddata = 0; // two bytes concatenated from inputstream
long angle = 0; // holds processed angle value
long anglemask = 262080; // 0x111111111111000000: mask to obtain first 12 digits with position info
long statusmask = 63; // 0x000000000000111111; mask to obtain last 6 digits containing status info
long statusbits; // holds status/error information
int DECn; // bit holding decreasing magnet field error data
int INCn; // bit holding increasing magnet field error data
int OCF; // bit holding startup-valid bit
int COF; // bit holding cordic DSP processing error data
int LIN; // bit holding magnet field displacement error data
int debug = 0; // SET THIS TO 0 TO DISABLE PRINTING OF ERROR CODES

void setup()
{
Serial.begin(57600);
pinMode(ledPin, OUTPUT); // visual signal of I/O to chip: heartbeat
pinMode(clockPin, OUTPUT); // SCK
pinMode(CSnPin, OUTPUT); // CSn -- has to toggle high and low to signal chip to start data transfer
pinMode(inputPin, INPUT); // SDA
}

void loop()
{
// CSn needs to cycle from high to low to initiate transfer. Then clock cycles. As it (clock) goes high
// again, data will appear on sda
digitalWrite(CSnPin, HIGH); // CSn high
digitalWrite(clockPin, HIGH); // CLK high
delay(1); // wait for 1 second for no particular reason
digitalWrite(ledPin, HIGH); // signal start of transfer with LED
digitalWrite(CSnPin, LOW); // CSn low: start of transfer
delay(1); // delay for chip -- 1000x longer than it needs to be
digitalWrite(clockPin, LOW); // CLK goes low: start clocking
delay(1); // hold low for 10 ms
for (int x=0; x < 18; x++) // clock signal, 18 transitions, output to clock pin
{
digitalWrite(clockPin, HIGH); // clock goes high
delay(1); // wait 10ms
inputstream = digitalRead(inputPin); // read one bit of data from pin
packeddata = ((packeddata) > 6); // shift 18-digit angle right 6 digits to form 12-digit value
angle = angle * 0.08789; // angle * (360/4096) == actual degrees
Serial.print("angle: "); // and, finally, print it.
Serial.println(angle, DEC);
}
if (debug)
{
statusbits = packeddata & statusmask;
DECn = statusbits & 2; // goes high if magnet moved away from IC
INCn = statusbits & 4; // goes high if magnet moved towards IC
LIN = statusbits & 8; // goes high for linearity alarm
COF = statusbits & 16; // goes high for cordic overflow: data invalid
OCF = statusbits & 32; // this is 1 when the chip startup is finished.
if (DECn && INCn) { Serial.println("magnet moved out of range"); }
else
{
if (DECn) { Serial.println("magnet moved away from chip"); }
if (INCn) { Serial.println("magnet moved towards chip"); }
}
if (LIN) { Serial.println("linearity alarm: magnet misaligned? Data questionable."); }
if (COF) { Serial.println("cordic overflow: magnet misaligned? Data invalid."); }
}

packeddata = 0; // reset both variables to zero so they don't just accumulate
angle = 0;
}

not checked the code but this is certainly wrong

packeddata = ((packeddata) > 6); // shift 18-digit angle right 6 digits to form 12-digit value

should be

packeddata = packeddata >> 6; // shift 18-digit angle right 6 digits to form 12-digit value

furthermore CTRL-T reformats the code to be more readable

and please use comments only to explain the why, not to repeat the code
[bad comments]
digitalWrite(CSnPin, HIGH); // CSn high
digitalWrite(clockPin, HIGH); // CLK high

[good comments]
digitalWrite(ledPin, HIGH); // signal start of transfer with LED

[worst comments]
delay(1); // hold low for 10 ms <<< comment and code in conflict -> code wins!

Thank you for the correction. I suspect that there is a lot more incorrect or missing!
I don't understand how CTRL-T should be used.
I note your objections to the comments but it was not my code.

I don't understand how CTRL-T should be used.

Just once, in the IDE.

spiney:
I don't understand how CTRL-T should be used.
I note your objections to the comments but it was not my code.

Ctrl-t is used in the IDE, or you can use the menu: Tools-> Auto Format

Now's a great time to make it more your code.

Does your encoder look something like this? http://www.madscientisthut.com/Shopping/agora.cgi?cart_id=6920626.863&product=CNC%20/%20Robotic%20Sensors&user4=Rotary%20Encoder&xm=on

You are right about the lack of input into packetdata. I suspect there is code missing.
Have a look at http://www.madscientisthut.com/forum_php/search.php?keywords=AS5045+&sid=fd8b02efa82281e52fb5b5200251ccdb, there's a ppost by john that has some code in it that looks a lot better than what you have.

Thank you Lar3ry. I bought directly from AMS AG but the board is similar.
John's code will do nicely. Problem solved.

I note your objections to the comments but it was not my code.

It was meant to be constructive to improve your writing style,