Using "Binary" in "C"

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
Post Reply
StepFather
Baa!
Baa!
Posts: 47
Joined: Sun Oct 30, 2005 5:57 pm 0
Location: SPACE

Using "Binary" in "C"

Post by StepFather »

Cheers,

Is it me or is it insanity that "C" doesn't support BINARY??? 8O I mean you can print decimal numbers using %d, octal using %o and Hex using %x, where the heck is BINARY??? 8O
User avatar
pogo
Sheepie Whore
Sheepie Whore
Posts: 14180
Joined: Tue Sep 10, 2002 8:28 pm 0
Yahoo Messenger: BrassSparrow
Location: County Durham

Post by pogo »

StepFather
Baa!
Baa!
Posts: 47
Joined: Sun Oct 30, 2005 5:57 pm 0
Location: SPACE

Post by StepFather »

pogo wrote:Already done to death here:

http://www.yakyak.org/viewtopic.php?t=4 ... ght=binary
Cheers @Pogo!
That just proves my point .. native "C" doesn't support binary, but there are tricks to get around it :)
User avatar
wossname
Baa!
Baa!
Posts: 52
Joined: Fri Oct 07, 2005 8:46 pm 0

Post by wossname »

This only serves to prove that C is a load of crap and always has been. ASM is the master and always will be :D :D :D



DWORD cheapRestaurant 10111010101011011111000000001101b
I give good code.
StepFather
Baa!
Baa!
Posts: 47
Joined: Sun Oct 30, 2005 5:57 pm 0
Location: SPACE

Post by StepFather »

wossname wrote:This only serves to prove that C is a load of crap and always has been. ASM is the master and always will be :D :D :D



DWORD cheapRestaurant 10111010101011011111000000001101b
I agree all the way; Assembly is better than anything I have ever done. But landing a games industry job today with Assembly skills alone is worthless. You are EXPECTED to know at least C and in 99% of all cases also C++ and be an expert in Design Patterns.
User avatar
Firefox
Sheepie Whore
Sheepie Whore
Posts: 11412
Joined: Fri Apr 26, 2002 6:15 pm 0
Location: Sheffield

Post by Firefox »

I must admit that I am a little puzzled that they didn't fix this in C99. :?
StepFather
Baa!
Baa!
Posts: 47
Joined: Sun Oct 30, 2005 5:57 pm 0
Location: SPACE

Post by StepFather »

Firefox wrote:I must admit that I am a little puzzled that they didn't fix this in C99. :?
Yeah, it came as a surprise to me also. Especially considering that hardcore "generic C" is so widely used in Embedded programming at the lowest level for key products such as GBA and many more embedded devices.
User avatar
pogo
Sheepie Whore
Sheepie Whore
Posts: 14180
Joined: Tue Sep 10, 2002 8:28 pm 0
Yahoo Messenger: BrassSparrow
Location: County Durham

Post by pogo »

Firefox wrote:I must admit that I am a little puzzled that they didn't fix this in C99. :?
As everyone's used to using hex ? There's nothing to stop language vendors adding it - all compilers I use have vendor specific extensions, to deal with things not covered in the language spec, and add preprocessor and platform-specific capabilities. No-one's added binary that I know of so there can't be much call for it.
StepFather
Baa!
Baa!
Posts: 47
Joined: Sun Oct 30, 2005 5:57 pm 0
Location: SPACE

Post by StepFather »

pogo wrote:
Firefox wrote:I must admit that I am a little puzzled that they didn't fix this in C99. :?
As everyone's used to using hex ? There's nothing to stop language vendors adding it - all compilers I use have vendor specific extensions, to deal with things not covered in the language spec, and add preprocessor and platform-specific capabilities. No-one's added binary that I know of so there can't be much call for it.
@Pogo
What compilers do you use?
User avatar
tufty
Sheepie Whore
Sheepie Whore
Posts: 12525
Joined: Fri Jan 07, 2005 4:52 pm 0
Location: French Alps

Post by tufty »

pogo wrote:all compilers I use have vendor specific extensions, to deal with things not covered in the language spec, and add preprocessor and platform-specific capabilities. No-one's added binary that I know of so there can't be much call for it.
some precompilers include 0b..... as an directive for binary constants (I can't remember which, off the top of my head, but I've definitely seen it) and there are some versions of atol/ltoa et al that deal with binary too.

I have to say, though, that in 20+ years of programming C and related languages, I have never once needed to use binary. And the only time I've come across the 0b prefixed constants was when I had to fix code on a compiler change. Sure, it looks like an logical thing, C can deal with hex, decimal and octal, but in reality you simply don't need it 99.999% of the time, which is probably why C99 doesn't include a fix..

The only possible use I can think of is monitoring memory addresses for bit-level changes, and for that, if you want to dump out a byte / word / whatever in binary representation, it's a very simple macro, after all.

Simon
"massive retard™", "full of shit™", "tiny bitch™", "retarted™", "a bit of a one trick pony™", "petty™", "simple minded idealistic twat™", "miserable humourless fucker™"
User avatar
Flabio
Baaaaaaa!
Baaaaaaa!
Posts: 8717
Joined: Wed Mar 03, 2004 12:37 pm 0
Location: Guildford

Post by Flabio »

Binary constants could be useful for doing bit mask enumerations, but I just tend to do (1 << x) where x is the bit number.

The reason asm is not required for games coding these days is that every machine has a different one now, and I really don't think most people want to be coding x360 or ps3 entirely in assembler. If you can do asm, it's still a sought after skill (always room for optimising) but it's no longer a core skill requirement.

Digressing further, the currently in vogue skill for new entrants to the industry is skill in concurrent programming. Can't think why ;)
User avatar
Firefox
Sheepie Whore
Sheepie Whore
Posts: 11412
Joined: Fri Apr 26, 2002 6:15 pm 0
Location: Sheffield

Post by Firefox »

It would be of use to me in my admittedly niche system-on-chip programming exploits.

Such chips have literally hundreds of registers which each contain a dozen or more packed bitfields, so it'd be really nice to go "*xyzreg = (0b110 << 27) | (0b01110 << 23) | (0b11 << 15)..." or similar (though probably something a bit more self-documenting than that).

Instead I ended up writing a specialist calculator that could translate back and forth between the separate bitfields and the equivalent hex constant.

Oddly enough the GNU assembler supports the "0b" prefix (as well as the familar "0" for octal and "0x" for hex) but the GNU C compiler doesn't. :?
User avatar
pogo
Sheepie Whore
Sheepie Whore
Posts: 14180
Joined: Tue Sep 10, 2002 8:28 pm 0
Yahoo Messenger: BrassSparrow
Location: County Durham

Post by pogo »

Flabio wrote:The reason asm is not required for games coding these days is that every machine has a different one now, and I really don't think most people want to be coding x360 or ps3 entirely in assembler. If you can do asm, it's still a sought after skill (always room for optimising) but it's no longer a core skill requirement.
I think a good understanding of asm is still valuable, but not for coding in it. It's useful for debugging - sometimes it's only possible to step through code in (dis)assembly mode instead of source mode, e.g. when much of the code you're interested in has been inlined.

But it's even more important to know enough asm to avoid writing C code which runs inefficiently on a particular platform. You can learn a lot about what works well and what doesn't by just stepping through the compiled output of your code in the debugger (and often spot programmers who don't know assembler from their C code).

Anyway, back on topic, I have no problems dealing with bitfields and similar objects in hex most of the time. For 32 and 64 bit registers hex is much easier than parsing 32 or 64 zeros and ones. In code I'll use stuff like

Code: Select all

enum {bit1, bit2, bit3, bit4, bit5};
enum {mask1 = 1 << bit1, mask2 = 1 << bit2, ..., maskAll = mask1 | mask2 | ...};
with 'bitX' and 'maskX' replaced with appropriate names, making it easy to address any bit in code without binary.
StepFather wrote:@Pogo
What compilers do you use?
MS VCC, GCC (on the Mac and for consoles) mostly, Metrowerks C occasionally.
StepFather
Baa!
Baa!
Posts: 47
Joined: Sun Oct 30, 2005 5:57 pm 0
Location: SPACE

Post by StepFather »

StepFather wrote:@Pogo
What compilers do you use?
MS VCC, GCC (on the Mac and for consoles) mostly, Metrowerks C occasionally.[/quote]

@Pogo,
Thanx :) It appears that we use the exact same tools; except CodeWarrior which is no longer available from MetroWerks. Do you by any chance have experience with the Intel Compiler v9?
Post Reply