Binary Tutorial?

Hello everybody I'm new to Arduino and I'm getting used to C slowly. The bulk of my programming background is in programming PIC18's in machine assembly code.

My question is how do I incorporate binary and hex into Arduino and C? Can anybody provide a few good sample programs that can illustrate hex and binary being used in the Arduino environment? Is it even possible to interface to C using raw binary values?

At the moment I've got a row of 10 LEDs and a potentiometer. One lab i would like to try is a binary counter and another is rotating the POT and having the LEDs show the binary value of the POT's wiper.

byte = 0xff;
byte = b00000000;

for starters

Osgeld:
byte = 0xff;
byte = b00000000;

for starters

...as a google search for "hexadecimal in C" would have revealed.

byte = b00000000;

Close, but no oral carcinogens.

Osgeld:
byte = 0xff;
byte = b00000000;

for starters

That's not very helpful to me. Could you use this in a sample of code to illustrate proper use?

My question is how do I incorporate binary and hex into Arduino and C? Can anybody provide a few good sample programs that can illustrate hex and binary being used in the Arduino environment?

In standard C, you can cause a number to be interpreted as a hex number by preceding it with a "0x" prefix.
In most versions of gcc (which is used by Arduino) you can cause a number to be interpreted as binary by preceding it with a "0b" prefix.
In the Arduino environment, there are a set of binary one-byte constants defined using "B" as a prefix.

So, 192, B11000000, 0b11000000, and 0xC0 are all the same number.
0xDeadBeef is a valid 32bit hex constant. 0b101001000100001000001 is a fine binary number (but rather unreadable.)

Don't put leading zeros on your decimal numbers, though. That makes them be read as octal, so 010 would be the same as 8 ! (harkening back to the original PDP11 days, I guess.)

harkening back to the original PDP11 PDP-8 (and possibly earlier) days

Octal is still relevant today in Linux systems, for setting access privileges.

harkening back to the original PDP11 PDP-8 (and possibly earlier) days

Well, C was originally developed for the PDP11, wasn't it? Octal itself is certainly much older, but I hypothesize that its inclusion in C was directly because C was aimed at PDP11 "systems programming", and the PDP11 was defined/documented/etc in octal.

(The 8080 also had a fundamentally octal instruction set, and it always pissed me off that hex was so widely using for programming it.)

Hehe, I still have my S-100 bus 8085/8088 system. I remember doing everything in asm and hex.

westfw:

My question is how do I incorporate binary and hex into Arduino and C? Can anybody provide a few good sample programs that can illustrate hex and binary being used in the Arduino environment?

In standard C, you can cause a number to be interpreted as a hex number by preceding it with a "0x" prefix.
In most versions of gcc (which is used by Arduino) you can cause a number to be interpreted as binary by preceding it with a "0b" prefix.
In the Arduino environment, there are a set of binary one-byte constants defined using "B" as a prefix.

So, 192, B11000000, 0b11000000, and 0xC0 are all the same number.
0xDeadBeef is a valid 32bit hex constant. 0b101001000100001000001 is a fine binary number (but rather unreadable.)

Don't put leading zeros on your decimal numbers, though. That makes them be read as octal, so 010 would be the same as 8 ! (harkening back to the original PDP11 days, I guess.)

Ok that explains things more clearly thank you for that explanation!

westfw:

harkening back to the original PDP11 PDP-8 (and possibly earlier) days

Well, C was originally developed for the PDP11, wasn't it? Octal itself is certainly much older, but I hypothesize that its inclusion in C was directly because C was aimed at PDP11 "systems programming", and the PDP11 was defined/documented/etc in octal.

The PDP-11 had 8 registers and the instruction set tended to have registers at octal boundaries, so octal was a natural fit for the 11. The left over bit in the 16-bit instruction word often times was used to indicate whether the instruction operated on 16-bit words or 8-bit characters. I spent the summer of 1978 doing PDP-11 standalone/simulator code, and had to delve into the instruction set, including toggling in the boot code at the front panel with 3 finger octal.

Note, that before switching to first the PDP-7 and later the PDP-11, Ritchie and Thompson had been part of the Multics project which was on a 36-bit machine, where octal is more natural. Bell Labs withdrew from the Multics project and Ritchie/Thompson and others started working on an underused PDP-7 and C/UNIX were begun.