Go Down

Topic: Binary Tutorial? (Read 1 time) previous topic - next topic

Bosscheesemo

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.

Osgeld

byte = 0xff;
byte = b00000000;

for starters
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

fungus


byte = 0xff;
byte = b00000000;

for starters


...as a google search for "hexadecimal in C" would have revealed.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

AWOL

Quote
byte = b00000000;

Close, but no oral carcinogens.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Bosscheesemo


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?

westfw

Quote
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.)


AWOL

#6
Jun 18, 2013, 08:56 am Last Edit: Jun 18, 2013, 09:06 am by AWOL Reason: 1
Quote
harkening back to the original PDP11 PDP-8 (and possibly earlier) days


Octal is still relevant today in Linux systems, for setting access privileges.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

westfw

Quote
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.)

hiduino

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


Bosscheesemo


Quote
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!

MichaelMeissner


Quote
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.

Go Up