Go Down

### Topic: Binary Tutorial? (Read 3827 times)previous topic - next topic

#### Bosscheesemo

##### Jun 15, 2013, 08:32 am
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

#1
##### Jun 15, 2013, 09:35 am
byte = 0xff;
byte = b00000000;

for starters

#### fungus

#2
##### Jun 15, 2013, 12:06 pm

byte = 0xff;
byte = b00000000;

for starters

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### AWOL

#3
##### Jun 15, 2013, 12:50 pm
Quote
byte = b00000000;

Close, but no oral carcinogens.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

#### Bosscheesemo

#4
##### Jun 18, 2013, 08:04 am

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

#5
##### Jun 18, 2013, 08:49 am
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 amLast 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 (who) 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.
I speak for myself, not Arduino.

#### westfw

#7
##### Jun 18, 2013, 09:16 am
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

#8
##### Jun 18, 2013, 11:34 am
Hehe, I still have my S-100 bus 8085/8088 system.  I remember doing everything in asm and hex.

#### Bosscheesemo

#9
##### Jun 18, 2013, 06:47 pm

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

#10
##### Jun 19, 2013, 02:57 pm

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