Pages: [1] 2   Go Down
 Author Topic: Base prefix notation  (Read 1082 times) 0 Members and 1 Guest are viewing this topic.
Mid-Atlantic, USA
Online
Sr. Member
Karma: 21
Posts: 439
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
 « on: May 02, 2013, 02:28:12 pm » Bigger Smaller Reset

First, let me start by saying I'm not intending to start a Holy War(tm) between coding styles. Just curious and think it might be a nice distracting conversation from dealing with brain-dead newb issues, (and us newbies might learn a thing or two).

I'm a rank beginner in C (let alone C++), so most of what I'm going on is from various examples strewn about the web. I've noticed two similar prefixes for Binary values, "Bnnnnnnnn" and "0bnnnnnnnn". Since both seem to work, so I suppose neither is specifically wrong. I was just wondering why someone would use one over the other. Is it mostly what was learned first, or is there a logical reason for the preference.

Personally, I think I prefer the "0bnnnnnnnn" notation because it mirrors the "0xnn" notation that I'm used to using for Hex. But I also see an argument that "B" is so significantly different from "0x" that it is easier to not confuse the two. I don't think that I've ever used (or had a reason to use) Octal, other than learning that it lends itself really well to 6bit processors much like Hex works well for 8 (and multiples thereof) processors. I'm not even sure how to notate octal in C/C++. Is there a prefix for decimal that no one uses (because, really, we are beings with 10 fingers so decimal is the natural default)?

(Side note about the "beings with 10 fingers" comment... Back when I used to wait tables I developed a finger counting method that, while is actually base 5 (1,2,3,4,10,11,12,13,14,20, etc...), I called "groups of 5" that I used for counting number of chairs or people in a room. Quickly move my fingers behind my back, and then when back in the safety of the waiter's station or kitchen count 25 for the left thumb, 5 for each left finger, and 1 for each right digit. To the customers it just looks like I'm standing there looking over the room with my hands behind my back. My short-term memory for numbers is so short, I needed to keep track on my fingers so I didn't loose count if someone asked me where the bathroom is... So much for base 10 being default...)
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
 « Reply #1 on: May 02, 2013, 02:37:25 pm » Bigger Smaller Reset

Quote
Bnnnnnnnn
Is an Arduino nicety, and only good for eight bits, I think, and not portable.

0bxxxxx is a supported C "standard" ( I confess, I do not know its true standing), so should be reasonably portable, and goes beyond eight bits.
Though why anyone would consider expressing a literal of more than four bits in binary is beyond me.

Quote
Personally, I think I prefer the "0bnnnnnnnn" notation because it mirrors the "0xnn" notation
But octal notation (leading zero) mirrors neither, and catches out many.

Edit:
Quote
Side note about the "beings with 10 fingers" comment
...but most people count on their fingers in unary.  ( or up to 1023 for the binary-savvy)
 « Last Edit: May 02, 2013, 02:41:49 pm 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.

Des Moines, WA - USA
Offline
God Member
Karma: 25
Posts: 779
 « Reply #2 on: May 02, 2013, 02:38:13 pm » Bigger Smaller Reset

'0b' denotes a binary litteral to the compiler. While the other is provided somewhere in the Arduino API support headers as per processor macros.
 Logged

UK
Offline
Karma: 92
Posts: 3969
 « Reply #3 on: May 02, 2013, 02:40:53 pm » Bigger Smaller Reset

0bnnnnnnnn is C/C++

Bnnnnnnnn is a set of #defines in hardware/arduino/cores/arduino/binary.h:
Code:
#define B0 0
#define B00 0
#define B000 0
#define B0000 0
#define B00000 0
#define B000000 0
#define B0000000 0
#define B00000000 0
#define B1 1
#define B01 1
#define B001 1
...

If you want to be "proper", use 0bnnnnnnnn.

And base 5 is common in counting.  I, II, III, IIII, IIII
 Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Mid-Atlantic, USA
Online
Sr. Member
Karma: 21
Posts: 439
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
 « Reply #4 on: May 02, 2013, 02:43:32 pm » Bigger Smaller Reset

Quote
Bnnnnnnnn
Is an Arduino nicety, and only good for eight bits, I think, and not portable.

0bxxxxx is a supported C "standard" ( I confess, I do not know its true standing), so should be reasonably portable, and goes beyond eight bits.
Though why anyone would consider expressing a literal of more than four bits in binary is beyond me.

Quote
Personally, I think I prefer the "0bnnnnnnnn" notation because it mirrors the "0xnn" notation
But octal notation (leading zero) mirrors neither, and catches out many.

Edit:
Quote
Side note about the "beings with 10 fingers" comment
...but most people count on their fingers in unary.  ( or up to 1023 for the binary-savvy)

So... what is octal notation?
 Logged

UK
Offline
Karma: 92
Posts: 3969
 « Reply #5 on: May 02, 2013, 02:47:46 pm » Bigger Smaller Reset

A 0 (zero) on the front.

Code:
unsigned char v = 0274;
 Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Global Moderator
UK
Offline
Brattain Member
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
 « Reply #6 on: May 02, 2013, 02:48:13 pm » Bigger Smaller Reset

Quote
So... what is octal notation?
Quote
 « Last Edit: May 02, 2013, 02:53:20 pm 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.

Offline
Sr. Member
Karma: 9
Posts: 254
 « Reply #7 on: May 02, 2013, 02:50:14 pm » Bigger Smaller Reset

0b is native to the compiler, much like 0x for hex, but it tends to be compiler specific whether that notation is actually supported and I don't believe it is part of the official c standard (gcc I believe does support it, which is the basis for many compilers). All of the Bnnn values are actually defined in a header file (binary.h in your Arduino core library folder). I imagine these were included for maximum compatibility between compilers (or maybe compatibility between programmers). It doesn't really matter which you use, as long as you have a compatible compiler or the right includes.
In terms of octal, C has a pretty stupid/confusing notation:
Code:
int x=035; <-- x is decimal 29!!!
It's particularly annoying as if you use 0's to try and align your numbers you get a nasty surprise e.g:
Code:
int c1=134;
int c2=120;
int c3=076; <--- This is troublesome
int c4=098; <--- Ahhhh!!!!!! (actually throws a compiler error!)
int c5=111;

EDIT: LOL, took a bit long typing that out
 Logged

Mid-Atlantic, USA
Online
Sr. Member
Karma: 21
Posts: 439
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
 « Reply #8 on: May 02, 2013, 02:52:36 pm » Bigger Smaller Reset

0bnnnnnnnn is C/C++

Bnnnnnnnn is a set of #defines in hardware/arduino/cores/arduino/binary.h:
Code:
#define B0 0
#define B00 0
#define B000 0
#define B0000 0
#define B00000 0
#define B000000 0
#define B0000000 0
#define B00000000 0
#define B1 1
#define B01 1
#define B001 1
...

If you want to be "proper", use 0bnnnnnnnn.

So (as AWOL says) the Arduino "nicety" is actually a kludge that one should probably avoid if one wants to develop good habits to use outside of ArduinoLand(tm). I'll keep my habit of converting "B" to "0b" when cleaning up examples that I'm learning from...

Quote
And base 5 is common in counting.  I, II, III, IIII, IIII

Thinking back on it, that is probably where I got it from.

Edit: corrected quote tags... One of the few times I forget to preview my post, and I put a "/" in the wrong place...
 « Last Edit: May 02, 2013, 02:56:32 pm by Sembazuru » Logged

Mid-Atlantic, USA
Online
Sr. Member
Karma: 21
Posts: 439
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
 « Reply #9 on: May 02, 2013, 02:54:48 pm » Bigger Smaller Reset

Quote
So... what is octal notation?
Quote

D'oh! I should really read completely. All that slobber on my foot makes walking slippery.
 Logged

Mid-Atlantic, USA
Online
Sr. Member
Karma: 21
Posts: 439
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
 « Reply #10 on: May 02, 2013, 03:07:06 pm » Bigger Smaller Reset

Though why anyone would consider expressing a literal of more than four bits in binary is beyond me.

I've seen (and used) occasions where on a single byte the placement of the high bits is more important than the value. Particularly bit-masking for flags, it's easier (for me) to see the 6th, 4th, 2nd and 1st bits are high (counting 1-8 starting with LSb on the right) on 0b00101101 instead of 0x2D or 45 (let alone 055 (yes, I can learn)).
 Logged

Offline
Full Member
Karma: 2
Posts: 196
 « Reply #11 on: May 03, 2013, 12:50:17 pm » Bigger Smaller Reset

(Side note about the "beings with 10 fingers" comment... Back when I used to wait tables I developed a finger counting method that, while is actually base 5 (1,2,3,4,10,11,12,13,14,20, etc...), I called "groups of 5" that I used for counting number of chairs or people in a room. Quickly move my fingers behind my back, and then when back in the safety of the waiter's station or kitchen count 25 for the left thumb, 5 for each left finger, and 1 for each right digit. To the customers it just looks like I'm standing there looking over the room with my hands behind my back. My short-term memory for numbers is so short, I needed to keep track on my fingers so I didn't loose count if someone asked me where the bathroom is... So much for base 10 being default...)

There actually is a combination base-5 / base-10 method called "chisanbop".

Right fingers (except for thumb) = 1 unit per finger
Right thumb = 5 units
Left fingers (except for thumb) = 10 units per finger
Left thumb = 50 units

And as for losing track of numbers, I too am very distractable. Only for me it's not limited to numbers.
I wonder: why is it considered less socially acceptable to refuse to acknowledge someone who is interrupting you, than it is to interrupt someone? Why isn't the interrupter the "bad guy"?
 Logged

Offline
Edison Member
Karma: 18
Posts: 1170
 « Reply #12 on: May 03, 2013, 12:59:48 pm » Bigger Smaller Reset

Quote
int c3=076; <--- This is troublesome
int c4=098; <--- Ahhhh!!!!!! (actually throws a compiler error!)

I don't know why it always makes me crack up, but I love this comment in the 2nd edition of K&R:
Quote
Everyone's favorite trivial change: 8 and 9 are not octal digits

So pre-ANSI, your 098 might not have thrown an error, but I have no idea what it would do.
 Logged

Offline
Full Member
Karma: 2
Posts: 196
 « Reply #13 on: May 03, 2013, 01:00:20 pm » Bigger Smaller Reset

As for a leading zero for octal: I doubt that a worse piece of design in programming language syntax exists anywhere. Except maybe for those programming languages which number the months of the year from 0 to 11 (such as C and ECMAScript).

Let's see: I believe that the relevant rule in many programming languages (including the Arduino programming environment) goes like this:
Numbers with only decimal digits (0 through 9) are decimal.
Except if they begin with 0, in which case they are octal.
Except if they contain a decimal point, in which case they are decimal after all.
Please correct me if I am wrong.
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
 « Reply #14 on: May 03, 2013, 01:06:25 pm » Bigger Smaller Reset

Quote
Except maybe for those programming languages which number the months of the year from 0 to 11 (such as C and ECMAScript).
I believe if you read the C language standard, you will find not a single reference to months of the year, or how they should be subscripted.
 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.

 Pages: [1] 2   Go Up