Pages: [1]   Go Down
Author Topic: Binary Tutorial?  (Read 985 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

byte = 0xff;
byte = b00000000;

for starters
Logged


Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5469
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

byte = 0xff;
byte = b00000000;

for starters

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26250
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
byte = b00000000;
Close, but no oral carcinogens.
Logged

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

Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 132
Posts: 6746
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26250
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 18, 2013, 02:06:54 am by AWOL » Logged

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 132
Posts: 6746
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Wahiawa, Hawaii
Offline Offline
God Member
*****
Karma: 29
Posts: 589
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: