stryd_one Posted March 28, 2007 Report Share Posted March 28, 2007 ahhh am I doing something wrong? should this work?unsigned long MyVar;MyVar = 0b10000000000000000000000000000001;I get:Syntax Error: token -> b10000000000000000000000000000001I hate learning new lingo. Quote Link to comment Share on other sites More sharing options...
audiocommander Posted March 28, 2007 Report Share Posted March 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
stryd_one Posted March 28, 2007 Author Report Share Posted March 28, 2007 Hmmm...Newbie Qs Use binary instead of HexWell that was entirely uninformative. LOLEdit: 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:42SDCC is a C compiler. 0b11111110 is not allowed in C. Quote Link to comment Share on other sites More sharing options...
stryd_one Posted March 28, 2007 Author Report Share Posted March 28, 2007 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? Quote Link to comment Share on other sites More sharing options...
audiocommander Posted March 28, 2007 Report Share Posted March 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
stryd_one Posted March 28, 2007 Author Report Share Posted March 28, 2007 D'oh. Wikipedia has spoken:C does not directly support binary notationhmmm... 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 ;) Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 19, 2007 Author Report Share Posted June 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
th0mas Posted June 20, 2007 Report Share Posted June 20, 2007 That's ugly and slow. Just use hex :PIt's really easy! every four bits turns into one hex digit. Quote Link to comment Share on other sites More sharing options...
TK. Posted June 20, 2007 Report Share Posted June 20, 2007 It's sometimes better to use shifted ones for creating a pattern, e.g.(1 << 0)is the same like 0b00000001or (1 << 2)is the same like 0b00000100advantage: 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 0b10000001And if you are looking for the inverted result, write:~((1 << 0) | (1 << 7))is: 0b01111110Also 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: 0b00011011Best Regards, Thorsten. Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 21, 2007 Author Report Share Posted June 21, 2007 That's ugly and slow. Just use hex :PIt'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 :) Quote Link to comment Share on other sites More sharing options...
th0mas Posted June 21, 2007 Report Share Posted June 21, 2007 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 SmileyLooking 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.