.TH "binary_constants.h" 3 "Fri Dec 9 2011" "Version 1.5" "Binary constants" \" -*- nroff -*- .ad l .nh .SH NAME binary_constants.h \- .PP Enter binary constants in a legible way that only uses standard language features and thus works with every standard-compliant compiler. .SH SYNOPSIS .br .PP .SS "Defines" .in +1c .ti -1c .RI "#define \fBHEX__\fP(n) 0x##n##LU" .br .ti -1c .RI "#define \fBB8__\fP(x)" .br .ti -1c .RI "#define \fBB8\fP(d) ((unsigned char)B8__(HEX__(d)))" .br .RI "\fIFor up to 8-bit binary constants. \fP" .ti -1c .RI "#define \fBB16\fP(dmsb, dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))" .br .RI "\fIFor up to 16-bit binary constants, MSByte first. \fP" .ti -1c .RI "#define \fBB32\fP(dmsb, db2, db1, dlsb)" .br .RI "\fIFor up to 32-bit binary constants, MSByte first. \fP" .ti -1c .RI "#define \fBB64\fP(dmsb, db6, db5, db4, db3, db2, db1, dlsb)" .br .RI "\fIFor up to 64-bit binary constants, MSByte first. \fP" .in -1c .SH "Detailed Description" .PP Enter binary constants in a legible way that only uses standard language features and thus works with every standard-compliant compiler. Enter binary constants in C/C++ source like this: var = \fBB8(01011100)\fP; .PP All macros evaluate to compile-time constants. Macros: .PP \fBB8(01010101)\fP = 0x55 = 85 .PP \fBB16(10101010,01010101)\fP = 0xAA55 = 43605 = -21931 .PP \fBB32(10000000,11111111,10101010,01010101)\fP = 0x80FFAA55 = 2164238933 = -2130728363 .PP \fBB64(11100011,11001100,11100010,11000110,10000000,11111111,10101010,01010101)\fP = 0xE3CCE2C680FFAA55 = 16414744084054256213 = -2031999989655295403 .PP Obtained from http://bytes.com/forum/thread216333.html .br (or http://www.velocityreviews.com/forums/t318127-using-binary-numbers-in-c.html as macros.h) .PP Binary constant generator macro. By Tom Torfs - donated to the public domain. Improvements by Volker Kuhlmann - also in the public domain. .PP Download: .PP \fBSee also:\fP .RS 4 http://volker.top.geek.nz/soft/ .RE .PP History: .IP "\(bu" 2 v. 1.1VK 15 Oct 2007 .IP " \(bu" 4 Tidied up into .h file, added \fBB32()\fP, \fBB64()\fP, improved test code. .PP .IP "\(bu" 2 v. 1.2VK 01 Mar 2008 .IP " \(bu" 4 Added broken compiler test, improved test code. .PP .IP "\(bu" 2 v. 1.3VK 29 Mar 2008 .IP " \(bu" 4 Added surrounding conditional. .PP .IP "\(bu" 2 v. 1.4VK 02 Apr 2008 .IP " \(bu" 4 Added missing () in \fBB8__()\fP (thanks lint). Version number. .PP .IP "\(bu" 2 v. 1.5VK 07 Dec 2011 .IP " \(bu" 4 Added doxygen markup. .PP .PP .SH "Define Documentation" .PP .SS "#define B16(dmsb, dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))" .PP For up to 16-bit binary constants, MSByte first. .SS "#define B32(dmsb, db2, db1, dlsb)"\fBValue:\fP .PP .nf (((unsigned long)B8(dmsb)<<24) \ + ((unsigned long)B8(db2)<<16) \ + ((unsigned long)B8(db1)<<8) \ + B8(dlsb)) .fi .PP For up to 32-bit binary constants, MSByte first. .SS "#define B64(dmsb, db6, db5, db4, db3, db2, db1, dlsb)"\fBValue:\fP .PP .nf (((unsigned long long)B32(dmsb,db6,db5,db4)<<32) \ + B32(db3,db2,db1,dlsb)) .fi .PP For up to 64-bit binary constants, MSByte first. .SS "#define B8(d) ((unsigned char)B8__(HEX__(d)))" .PP For up to 8-bit binary constants. .SS "#define B8__(x)"\fBValue:\fP .PP .nf (((x&0x0000000FLU)?1:0) \ +((x&0x000000F0LU)?2:0) \ +((x&0x00000F00LU)?4:0) \ +((x&0x0000F000LU)?8:0) \ +((x&0x000F0000LU)?16:0) \ +((x&0x00F00000LU)?32:0) \ +((x&0x0F000000LU)?64:0) \ +((x&0xF0000000LU)?128:0)) .fi .SS "#define HEX__(n) 0x##n##LU" .SH "Author" .PP Generated automatically by Doxygen for Binary constants from the source code.