Download code

Jump to: navigation, search

Back to Handy_macros_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

macro_iseven.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Handy_macros_(C)?oldid=15577
14 */
15 
16 #include <stdio.h>
17 
18 #define iseven(value) (!((value)&0x01))
19 
20 int main(void)
21 {
22  int n;
23  for(n=0; n<65; n++)
24  {
25   printf("%d is%s even\n", n, iseven(n)?"":" not");
26  }
27  return 0;
28 }


hijacker
hijacker
hijacker
hijacker

macro_bounds.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Handy_macros_(C)?oldid=15577
14 */
15 
16 
17 #include <stdio.h>
18 
19 #define bounds(value, min, max)  ((TEMP_VALUE__=(value))>(TEMP_MAX__=(max))?TEMP_MAX__:(TEMP_VALUE__<(TEMP_MIN__=(min))?TEMP_MIN__:TEMP_VALUE__))
20 
21 int main(void)
22 {
23   int n;
24   int TEMP_VALUE__, TEMP_MAX__, TEMP_MIN__;
25   for(n=0; n<10; n++)
26   {
27     printf("n = %d Adjusted: %d\n", n, bounds(n,3,6));
28   }
29   return 0;
30 }
31 


hijacker
hijacker
hijacker
hijacker

macro_powoftwo.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Handy_macros_(C)?oldid=15577
14 */
15 
16 
17 #include <stdio.h>
18 
19 #define ispoweroftwo(value) ((TEMP_VALUE__=(value))&&!((TEMP_VALUE__-1)&TEMP_VALUE__))
20 
21 int main(void)
22 {
23   int n;
24   int TEMP_VALUE__;
25   for(n=0; n<65; n++)
26   {
27     printf("%d is%s power of two\n",n,ispoweroftwo(n)?"":" not");
28   }
29   return 0;
30 }
31 


hijacker
hijacker
hijacker
hijacker

macro_clamptoone.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Handy_macros_(C)?oldid=15577
14 */
15 
16 
17 #include <stdio.h>
18 
19 #define clamptoone(value) (!!(value))
20 
21 int main(void)
22 {
23   int n;
24   for(n=0; n<4; n++)
25   {
26     printf("n = %d clamped = %d\n",n,clamptoone(n));
27   }
28   return 0;
29 }
30 


hijacker
hijacker
hijacker
hijacker

macro_xorswap.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Handy_macros_(C)?oldid=15577
14 */
15 
16 
17 #include <stdio.h>
18 
19 #define xorswap(a, b) ((a)^=(b),(b)^=(a),(a)^=(b))
20 
21 int main()
22 {
23   int n1=5;
24   int n2=7;
25 
26   printf("Before xorswap() n1=%d, n2=%d\n", n1, n2);
27   xorswap(n1, n2);
28   printf("After xorswap() n1=%d, n2=%d\n", n1, n2);
29 
30   return 0;
31 }
32 


hijacker
hijacker
hijacker
hijacker