Go Down

Topic: Base prefix notation (Read 2 times) previous topic - next topic

Sembazuru

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

AWOL

#1
May 02, 2013, 09:37 pm Last Edit: May 02, 2013, 09:41 pm by AWOL Reason: 1
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)
"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.

lloyddean

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

majenko

0bnnnnnnnn is C/C++

Bnnnnnnnn is a set of #defines in hardware/arduino/cores/arduino/binary.h:
Code: [Select]

#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
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Sembazuru


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?

Go Up