Jump to content

Defining an unsigned long as binary.. What am I doing wrong?


stryd_one
 Share

Recommended Posts

Hey Stryd,

I'm not sure if one can write this way (0b00001) in C or if it's supported by SDCC. I have found no reference, except for a codesnippet of the MM application (v-pot) and there the ASM-style is out-commented:


const unsigned int preset_patterns[32] = {
0x0001, // b'0000000000000001'
0x0001, // b'0000000000000001'
0x0003, // b'0000000000000011'
0x0003, // b'0000000000000011'
0x0003, // b'0000000000000011'
0x0007, // b'0000000000000111'
0x0007, // b'0000000000000111'
0x0007, // b'0000000000000111'
0x000f, // b'0000000000001111'
0x000f, // b'0000000000001111'
0x000f, // b'0000000000001111'
0x001f, // b'0000000000011111'
0x001f, // b'0000000000011111'
0x001f, // b'0000000000011111'
0x003f, // b'0000000000111111'
0x003f, // b'0000000000111111'
0x003f, // b'0000000000111111'
0x003f, // b'0000000000111111'
0x007f, // b'0000000001111111'
0x007f, // b'0000000001111111'
0x007f, // b'0000000001111111'
0x00ff, // b'0000000011111111'
0x00ff, // b'0000000011111111'
0x00ff, // b'0000000011111111'
0x01ff, // b'0000000111111111'
0x01ff, // b'0000000111111111'
0x01ff, // b'0000000111111111'
0x03ff, // b'0000001111111111'
0x03ff, // b'0000001111111111'
0x03ff, // b'0000001111111111'
0x07ff, // b'0000011111111111'
0x07ff, // b'0000011111111111'
};
[/code]

Or does it work with lower numbers like shorts and chars?

If that's the case, you might add an 'int' after the long and give it a new try.

I can vaguely remember I tried a binary notation, too, but failed.

Then I expect you added the sdcc lib, right? (it needs _fs2ulong.c to deal with longs);

Cheers,

Michael

Link to comment
Share on other sites

Hmmm...

Newbie Qs Use binary instead of Hex

Well that was entirely uninformative. LOL

Edit: I'll save you a click:

I try to convert the following statement

P1 = 0xfe; /* ok */

/* to */

P1 = 0b11111110; /* not ok */

but the complier complain.

Is SDCC not support binary?

or i need to use some other method?

By: Maarten Brock (maartenbrock) - 2004-08-26 05:42

SDCC is a C compiler. 0b11111110 is not allowed in C.

Link to comment
Share on other sites

I hope this page is wrong:

Odd things about C/C++

Sunday, February 8th, 2004

    * There is no syntax for describing a number in binary notation. You can express a number in decimal [65], hexadecimal [0×41], octal [0101] or as a character-constant [’A], but there is no binary equivalent [eg 0b1000001]. Seriously, who needs octal more than binary?

And yet you can still bitshift. WTF?

Link to comment
Share on other sites

mmm, yeah, binary would indeed be a bit more practical than octal :-\

for lower numbers one can set up a bitfield, that may be a bit of a workaround, but with SDCC only 8bit-bitfields are allowed.

so, I guess, you have to learn hex (I still can't think in hex... always using my calculator)...

;) Michael

Link to comment
Share on other sites

D'oh. Wikipedia has spoken:

C does not directly support binary notation

hmmm... That's a small spanner in my works. No bit type, no binary. I feel __ASM coming on ;)

Edit: well it'll probably be generated and read by bitshifting so hopefully it will be transparent and I won't have to learn hex by heart, but I'll probably see so much of it during debugging that I'll learn the hex anyway ;)

Link to comment
Share on other sites

  • 2 months later...

Well I have long since worked around this, but today I stumbled across the answer...doh! Thought I'd share it for future reference anyway:

You use defines to pre-processor convert your binary to hex.

#define BIN(x)                              \
  (  ((0x##x##L & 0x00000001L) ? 0x01 : 0)  \
   | ((0x##x##L & 0x00000010L) ? 0x02 : 0)  \
   | ((0x##x##L & 0x00000100L) ? 0x04 : 0)  \
   | ((0x##x##L & 0x00001000L) ? 0x08 : 0)  \
   | ((0x##x##L & 0x00010000L) ? 0x10 : 0)  \
   | ((0x##x##L & 0x00100000L) ? 0x20 : 0)  \
   | ((0x##x##L & 0x01000000L) ? 0x40 : 0)  \
   | ((0x##x##L & 0x10000000L) ? 0x80 : 0))

T2CON = BIN(00110100);

via: http://sourceforge.net/tracker/index.php?func=detail&aid=882215&group_id=599&atid=350599

Link to comment
Share on other sites

It's sometimes better to use shifted ones for creating a pattern, e.g.

(1 << 0)

is the same like 0b00000001

or (1 << 2)

is the same like 0b00000100

advantage: everybody who knows this coding style can find out the bit which is set immediately (first example: bit 0, second example; bit 2)

Combinations are possible as well:

(1 << 0) | (1 << 7)

is 0b10000001

And if you are looking for the inverted result, write:

~((1 << 0) | (1 << 7))

is: 0b01111110

Also bitfields can be set this way. E.g., if 3 2-bit fields are stored in a byte, you could write:

(1 << 4) | (2 << 2) | (3 << 0)

is: 0b00011011

Best Regards, Thorsten.

Link to comment
Share on other sites

That's ugly and slow. Just use hex :P

It's really easy!  every four bits turns into one hex digit.

Totally that's what I started doing, until I saw TK's trick above in some of his code and started doing it that way. It's much nicer for bitfields as he mentioned.

It's not slow though, that's a #define, so it all gets converted to hex before compile time and long before run time :)

Link to comment
Share on other sites

Agreed that bitshifting is the best way to set your fields compared to hex constants.

It's not slow though, that's a #define, so it all gets converted to hex before compile time and long before run time Smiley

Looking closer, you're right.  I was thinking the macro would get expanded into the code but reading the declaration closer it'll all get processed in the preprocessor and just the new hex value will be put in your code.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...