Go to Post If the fan club goes away, or isn't always right in your face doing things the public sees, does that mean the reason for the fan club is gone? Nope. - Elgin Clock [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 11-11-2005, 17:45
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
binary in C

Does anyone know if it's possible to use binary numbers in C? Of course, it's just to make it easier to read the source. I'm doing some bitwise operations and 218 or 0xDA just isn't as meaningful as 11011010.
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
  #2   Spotlight this post!  
Unread 11-11-2005, 18:28
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: binary in C

Quote:
Originally Posted by sciguy125
Does anyone know if it's possible to use binary numbers in C? Of course, it's just to make it easier to read the source. I'm doing some bitwise operations and 218 or 0xDA just isn't as meaningful as 11011010.
I don't believe the C standards have any support for entering numbers in binary. Just use hex - it'll be awkward for a bit but after a while you'll be able to read hex and "see" the binary without any trouble. Binary becomes cumbersome when you start talking about 16 and 32 bit (and larger) numbers too.

Most people who write a lot of software will tell you that 0xDA is much easier to read than 11011010.
  #3   Spotlight this post!  
Unread 11-11-2005, 19:41
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: binary in C

I'm 1200 miles from my K&R right now...

0b11011010

should be equivalent to

0xDA

N'est-ce pas?
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #4   Spotlight this post!  
Unread 11-11-2005, 19:58
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: binary in C

Quote:
Originally Posted by Mike Betts
I'm 1200 miles from my K&R right now...

0b11011010

should be equivalent to

0xDA
That's what I thought at first too, but gcc didn't seem to like it, and I found several discussions on some C newsgroups that suggested that binary constants aren't officially part of the language. However, I just checked it and MCC18 seemed to take it...

My advice still applies though: get used to hex, it's what everyone else uses
  #5   Spotlight this post!  
Unread 11-11-2005, 20:07
Manoel's Avatar
Manoel Manoel is offline
Registered User
FRC #0383 (Brazilian Machine)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 2000
Location: Porto Alegre, RS, Brazil
Posts: 608
Manoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond repute
Send a message via ICQ to Manoel Send a message via MSN to Manoel
Re: binary in C

Quote:
Originally Posted by K&R, page 37
"The value of an integer can be specified in octal or hexadecimal instead of decimal. A leading 0 (zero) on an integer constant means octal; a leading 0x or 0X means hexadecimal".
So apparently you can't, unless the C18 compiler is different in that aspect (it isn't a 100% ANSI C, after all) - assuming you are asking this about the Microchip compiler.
I agree with Dave that the hexadecimal form is much better once you get used to it.
You probably know it, but for the ones that do not, each hex symbol represents 4 bits. Once you realize that, it just isn't magic anymore.
__________________
Manoel Flores da Cunha
Mentor
Brazilian Machine
Team # 383
  #6   Spotlight this post!  
Unread 11-11-2005, 20:11
ghansel ghansel is offline
my amp goes to eleven
AKA: George H.
FRC #0019 (Big Red Robotics)
Team Role: Engineer
 
Join Date: Jan 2005
Location: Connecticut
Posts: 38
ghansel is infamous around these parts
Re: binary in C

0b should work. It's not ANSI C, but many compilers understand it. gcc compiles it fine. Dunno about C18 - I usually go out of my way to avoid binary.

http://refcards.com/refcards/c/c-refcard-letter.pdf

George
  #7   Spotlight this post!  
Unread 11-11-2005, 20:41
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
Re: binary in C

Well, I'm using SDCC. I'd use C18, but it doesn't work for the 16F series PICs. I just tried 0b and it didn't like it. I guess I'll have to stick to hex or decimal.
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
  #8   Spotlight this post!  
Unread 12-11-2005, 01:09
Matt Krass's Avatar
Matt Krass Matt Krass is offline
"Old" and Cranky. Get off my lawn!
AKA: Dark Ages
FRC #0263 (Sachem Aftershock)
Team Role: Mentor
 
Join Date: Oct 2002
Rookie Year: 2002
Location: Long Island, NY
Posts: 1,187
Matt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond repute
Send a message via AIM to Matt Krass
Re: binary in C

The gcc port for Atmel AVRs supports it no questions asked, it's actually fairly used in the examples I saw so 0b works there.....

No support in SDCC? Darn I was gonna use it for this 16F I got sitting here. Oh well...
__________________
Matt Krass
If I suggest something to try and fix a problem, and you don't understand what I mean, please PM me!

I'm a FIRST relic of sorts, I remember when we used PBASIC and we got CH Flightsticks in the KoP. In my day we didn't have motorized carts, we pushed our robots uphill, both ways! (Houston 2003!)
  #9   Spotlight this post!  
Unread 13-11-2005, 15:39
JJG13's Avatar
JJG13 JJG13 is offline
Yoda
AKA: Joshua Graffman
None #0180 (SPAM)
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2003
Location: Martin County, Florida
Posts: 105
JJG13 has a spectacular aura aboutJJG13 has a spectacular aura aboutJJG13 has a spectacular aura about
Re: binary in C

I sometimes use a calculator to convert from binary to hex and then write the binary equivalent in a comment by the hexadecimal value. For example:
Code:
int bit2mask = 0x04; //00000100b
__________________
Behold the power of SPAM.
  #10   Spotlight this post!  
Unread 13-11-2005, 22:12
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: binary in C

Code:
#define b00000000 0x00
#define b00000001 0x01
#define b00000010 0x02
#define b00000011 0x03
 :
 :
#define b11111110 0xFE
#define b11111111 0xFF
Generate 256 lines once, then let the preprocessor take care of it for you from then on.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary in Rose is Rose MisterX Chit-Chat 8 11-02-2004 18:26
pic: Dawning of a New Age. CD47-Bot Extra Discussion 90 31-07-2003 22:04
Fun with sensors Team852 Technical Discussion 7 06-01-2003 00:07
A couple of noodle scratchers Cheese Head Programming 11 07-12-2002 09:59
delphi: binary sort archiver 2000 0 24-06-2002 00:30


All times are GMT -5. The time now is 06:10.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi