Page 1 of 2
hard-coding binary constants
Posted: 28 Aug 2012, 17:47
by mattallen37
I think I have previously mentioned my wish for binary constants to be supported. I would type them out (or make the NXT type them), like this:
#define 0b00000000 0x00
#define 0b00000001 0x01
#define 0b00000010 0x02
#define 0b00000011 0x03
#define 0b00000100 0x04
#define 0b00000101 0x05
#define 0b00000110 0x06
#define 0b00000111 0x07
#define 0b00001000 0x08
#define 0b00001001 0x09
#define 0b00001010 0x0A
but you can't start a macro with a number (like "0").
I don't need 1, 2, 3, 5, 6, or 7 bits supported, but 4 and 8 bits would be great (e.g. 0b1010 and 0b10010110). Edit: actually a parser would be better (so that it would support any number of bits)
Re: hard-coding binary constants
Posted: 28 Aug 2012, 21:20
by afanofosc
Just so you know, there is not any support for binary literals in the C/C++ standard, from what I can gather via Google. C#, similarly, does not support binary literals. Java recently added support for binary literals in Java 7.
In spite of that, I'll have a look at adding support for binary literals in NXC.
John Hansen
Re: hard-coding binary constants
Posted: 28 Aug 2012, 23:11
by mattallen37
Interesting, because out of all the languages I have tried it in, NXC is the only one that doesn't support it yet. I know it works in WinAVR C, PICAXE Basic, ROBOTC C/C++, Arduino C/C++ etc.
Thank you for considering it!
Re: hard-coding binary constants
Posted: 28 Aug 2012, 23:21
by mattallen37
BTW, just for the fun of it, I made the NXT type out all of the 8-bit possibilities:
Code: Select all
#define 0b00000000 0x00
#define 0b00000001 0x01
#define 0b00000010 0x02
#define 0b00000011 0x03
#define 0b00000100 0x04
#define 0b00000101 0x05
#define 0b00000110 0x06
#define 0b00000111 0x07
#define 0b00001000 0x08
#define 0b00001001 0x09
#define 0b00001010 0x0A
#define 0b00001011 0x0B
#define 0b00001100 0x0C
#define 0b00001101 0x0D
#define 0b00001110 0x0E
#define 0b00001111 0x0F
#define 0b00010000 0x10
#define 0b00010001 0x11
#define 0b00010010 0x12
#define 0b00010011 0x13
#define 0b00010100 0x14
#define 0b00010101 0x15
#define 0b00010110 0x16
#define 0b00010111 0x17
#define 0b00011000 0x18
#define 0b00011001 0x19
#define 0b00011010 0x1A
#define 0b00011011 0x1B
#define 0b00011100 0x1C
#define 0b00011101 0x1D
#define 0b00011110 0x1E
#define 0b00011111 0x1F
#define 0b00100000 0x20
#define 0b00100001 0x21
#define 0b00100010 0x22
#define 0b00100011 0x23
#define 0b00100100 0x24
#define 0b00100101 0x25
#define 0b00100110 0x26
#define 0b00100111 0x27
#define 0b00101000 0x28
#define 0b00101001 0x29
#define 0b00101010 0x2A
#define 0b00101011 0x2B
#define 0b00101100 0x2C
#define 0b00101101 0x2D
#define 0b00101110 0x2E
#define 0b00101111 0x2F
#define 0b00110000 0x30
#define 0b00110001 0x31
#define 0b00110010 0x32
#define 0b00110011 0x33
#define 0b00110100 0x34
#define 0b00110101 0x35
#define 0b00110110 0x36
#define 0b00110111 0x37
#define 0b00111000 0x38
#define 0b00111001 0x39
#define 0b00111010 0x3A
#define 0b00111011 0x3B
#define 0b00111100 0x3C
#define 0b00111101 0x3D
#define 0b00111110 0x3E
#define 0b00111111 0x3F
#define 0b01000000 0x40
#define 0b01000001 0x41
#define 0b01000010 0x42
#define 0b01000011 0x43
#define 0b01000100 0x44
#define 0b01000101 0x45
#define 0b01000110 0x46
#define 0b01000111 0x47
#define 0b01001000 0x48
#define 0b01001001 0x49
#define 0b01001010 0x4A
#define 0b01001011 0x4B
#define 0b01001100 0x4C
#define 0b01001101 0x4D
#define 0b01001110 0x4E
#define 0b01001111 0x4F
#define 0b01010000 0x50
#define 0b01010001 0x51
#define 0b01010010 0x52
#define 0b01010011 0x53
#define 0b01010100 0x54
#define 0b01010101 0x55
#define 0b01010110 0x56
#define 0b01010111 0x57
#define 0b01011000 0x58
#define 0b01011001 0x59
#define 0b01011010 0x5A
#define 0b01011011 0x5B
#define 0b01011100 0x5C
#define 0b01011101 0x5D
#define 0b01011110 0x5E
#define 0b01011111 0x5F
#define 0b01100000 0x60
#define 0b01100001 0x61
#define 0b01100010 0x62
#define 0b01100011 0x63
#define 0b01100100 0x64
#define 0b01100101 0x65
#define 0b01100110 0x66
#define 0b01100111 0x67
#define 0b01101000 0x68
#define 0b01101001 0x69
#define 0b01101010 0x6A
#define 0b01101011 0x6B
#define 0b01101100 0x6C
#define 0b01101101 0x6D
#define 0b01101110 0x6E
#define 0b01101111 0x6F
#define 0b01110000 0x70
#define 0b01110001 0x71
#define 0b01110010 0x72
#define 0b01110011 0x73
#define 0b01110100 0x74
#define 0b01110101 0x75
#define 0b01110110 0x76
#define 0b01110111 0x77
#define 0b01111000 0x78
#define 0b01111001 0x79
#define 0b01111010 0x7A
#define 0b01111011 0x7B
#define 0b01111100 0x7C
#define 0b01111101 0x7D
#define 0b01111110 0x7E
#define 0b01111111 0x7F
#define 0b10000000 0x80
#define 0b10000001 0x81
#define 0b10000010 0x82
#define 0b10000011 0x83
#define 0b10000100 0x84
#define 0b10000101 0x85
#define 0b10000110 0x86
#define 0b10000111 0x87
#define 0b10001000 0x88
#define 0b10001001 0x89
#define 0b10001010 0x8A
#define 0b10001011 0x8B
#define 0b10001100 0x8C
#define 0b10001101 0x8D
#define 0b10001110 0x8E
#define 0b10001111 0x8F
#define 0b10010000 0x90
#define 0b10010001 0x91
#define 0b10010010 0x92
#define 0b10010011 0x93
#define 0b10010100 0x94
#define 0b10010101 0x95
#define 0b10010110 0x96
#define 0b10010111 0x97
#define 0b10011000 0x98
#define 0b10011001 0x99
#define 0b10011010 0x9A
#define 0b10011011 0x9B
#define 0b10011100 0x9C
#define 0b10011101 0x9D
#define 0b10011110 0x9E
#define 0b10011111 0x9F
#define 0b10100000 0xA0
#define 0b10100001 0xA1
#define 0b10100010 0xA2
#define 0b10100011 0xA3
#define 0b10100100 0xA4
#define 0b10100101 0xA5
#define 0b10100110 0xA6
#define 0b10100111 0xA7
#define 0b10101000 0xA8
#define 0b10101001 0xA9
#define 0b10101010 0xAA
#define 0b10101011 0xAB
#define 0b10101100 0xAC
#define 0b10101101 0xAD
#define 0b10101110 0xAE
#define 0b10101111 0xAF
#define 0b10110000 0xB0
#define 0b10110001 0xB1
#define 0b10110010 0xB2
#define 0b10110011 0xB3
#define 0b10110100 0xB4
#define 0b10110101 0xB5
#define 0b10110110 0xB6
#define 0b10110111 0xB7
#define 0b10111000 0xB8
#define 0b10111001 0xB9
#define 0b10111010 0xBA
#define 0b10111011 0xBB
#define 0b10111100 0xBC
#define 0b10111101 0xBD
#define 0b10111110 0xBE
#define 0b10111111 0xBF
#define 0b11000000 0xC0
#define 0b11000001 0xC1
#define 0b11000010 0xC2
#define 0b11000011 0xC3
#define 0b11000100 0xC4
#define 0b11000101 0xC5
#define 0b11000110 0xC6
#define 0b11000111 0xC7
#define 0b11001000 0xC8
#define 0b11001001 0xC9
#define 0b11001010 0xCA
#define 0b11001011 0xCB
#define 0b11001100 0xCC
#define 0b11001101 0xCD
#define 0b11001110 0xCE
#define 0b11001111 0xCF
#define 0b11010000 0xD0
#define 0b11010001 0xD1
#define 0b11010010 0xD2
#define 0b11010011 0xD3
#define 0b11010100 0xD4
#define 0b11010101 0xD5
#define 0b11010110 0xD6
#define 0b11010111 0xD7
#define 0b11011000 0xD8
#define 0b11011001 0xD9
#define 0b11011010 0xDA
#define 0b11011011 0xDB
#define 0b11011100 0xDC
#define 0b11011101 0xDD
#define 0b11011110 0xDE
#define 0b11011111 0xDF
#define 0b11100000 0xE0
#define 0b11100001 0xE1
#define 0b11100010 0xE2
#define 0b11100011 0xE3
#define 0b11100100 0xE4
#define 0b11100101 0xE5
#define 0b11100110 0xE6
#define 0b11100111 0xE7
#define 0b11101000 0xE8
#define 0b11101001 0xE9
#define 0b11101010 0xEA
#define 0b11101011 0xEB
#define 0b11101100 0xEC
#define 0b11101101 0xED
#define 0b11101110 0xEE
#define 0b11101111 0xEF
#define 0b11110000 0xF0
#define 0b11110001 0xF1
#define 0b11110010 0xF2
#define 0b11110011 0xF3
#define 0b11110100 0xF4
#define 0b11110101 0xF5
#define 0b11110110 0xF6
#define 0b11110111 0xF7
#define 0b11111000 0xF8
#define 0b11111001 0xF9
#define 0b11111010 0xFA
#define 0b11111011 0xFB
#define 0b11111100 0xFC
#define 0b11111101 0xFD
#define 0b11111110 0xFE
#define 0b11111111 0xFF
It probably took me 10 minutes to write the program, and it takes the NXT about 31 seconds to type.
Re: hard-coding binary constants
Posted: 11 Sep 2012, 04:27
by cghaning
I was thinking about this recently as well. I found this link below to the Arduino firmware source which contains binary representations for all values 0 to 255.
https://github.com/arduino/Arduino/blob ... o/binary.h
Re: hard-coding binary constants
Posted: 11 Sep 2012, 04:38
by mattallen37
That isn't "proper". It's supposed to be 0bnnnnnnnn instead of Bnnnnnnnn. Thanks for the link though.
Re: hard-coding binary constants
Posted: 11 Sep 2012, 12:41
by HaWe
here we go...
edit:
I actually was curious why no one else tried that before...
Re: hard-coding binary constants
Posted: 11 Sep 2012, 14:17
by afanofosc
I have already made changes to the compiler to support binary constants. Along with that I have made it so that BricxCC will treat .h files like .nxc files. I have also added the FLT_MAX constants to the header files, added support for exponential notation, added support for new DRAW options (clear eol, clear line), fixed string API function defects, and added TypeOf and FormatVal API functions.
I will upload a new test release later today, if all goes well.
John Hansen
Re: hard-coding binary constants
Posted: 11 Sep 2012, 14:23
by HaWe
*thx*
Re: hard-coding binary constants
Posted: 11 Sep 2012, 16:02
by spillerrec
mattallen37 wrote:Interesting, because out of all the languages I have tried it in, NXC is the only one that doesn't support it yet. I know it works in WinAVR C, PICAXE Basic, ROBOTC C/C++, Arduino C/C++ etc.
I checked the C and C++ specifications a year ago about this and only base 8, 10 and 16 is supported. While it is supported in quite a few compilers (for example GCC), it is just a compiler extension. (And AFAIK, several microprocessor 'languages' uses GCC behind the scenes.)
Looking forward to a new test release. : )