Last updated :
Posted :
The iso646.h header in c was defined as part of the c99 standard. It defines alternative ways for using the equality, boolean, and bitwise operators. For example instead of using the bitwise && operator, the and token can be used. iso646.h operators replacements, do not work in string or character literals.
#include <iso646.h>
/*
include the iso646 header,
in order to use the:
equality
boolean
bitwise
operators, alternative tokens .*/
#include <stdio.h>
/*
include the stdio.h header in
order to use printf, to print
formatted strings .*/
#include <stdbool.h>
/*
include stdbool.h header in
order to use boolean true,
and false.
true is defined as 1
false is defined as 0 .*/
#include <stdint.h>
/*
include the stdint header
in order to use u_int8_t, which
is an unsigned integer formed of
8 bits. */
char* boolToString( bool aBool );
/*
A function that converts a boolean
to string. */
int main( int argc, char const * argv[ ] ){
int anIntOne = 1;
int anIntwo = 2;
int anintThree = 1;
/* Equality operator alternative token
!= : not_eq .*/
printf( "%d not_eq %d = %s\n",
anIntOne,
anIntwo,
boolToString( anIntOne not_eq anIntwo ));
/* output :
1 not_eq 2 = true */
printf( "%d not_eq %d = %s\n",
anIntOne,
anintThree,
boolToString( anIntOne not_eq anintThree ));
/* output :
1 not_eq 1 = false .*/
/*
Boolean operators alternative tokens
&& : and
|| : or
! : not */
bool boolOne = true;
bool boolTwo = false;
// define two boolean values
printf( "%s and %s is %s\n",
boolToString( boolOne ),
boolToString( boolTwo ),
boolToString( boolOne and boolTwo ));
/* output :
true and false is false .*/
printf( "%s or %s is %s\n",
boolToString( boolOne ),
boolToString( boolTwo ),
boolToString( boolOne or boolTwo ));
/* output :
true or false is true .*/
printf( "not boolOne = not %s = %s\n",
boolToString( boolOne ),
boolToString( not boolOne ));
/* output :
not boolOne = not true = false .*/
/*
Bitwise operators alternative tokens
& : bitand
&= : and_eq
| : bitor
|= : or_eq
^ : xor
^= : xor_eq
~ : compl .*/
u_int8_t ui8_one = 3;
// ui8_one = 3 = 0000_0011
u_int8_t ui8_two = 5;
// ui8_two = 5 = 0000_0101
printf( "%d bitand %d = %d\n",
ui8_one,
ui8_two,
(u_int8_t)( ui8_one bitand ui8_two ));
/* output :
3 bitand 5 = 1
ui8_one : 0000_0011 : 3
ui8_two : 0000_0101 : 5
---------
0000_0001 : 1 .*/
printf( "%d and_eq %d = %d\n",
ui8_two,
ui8_one,
( u_int8_t )( ui8_two and_eq ui8_one ));
/* output :
5 and_eq 3 = 1
ui8_two : 0000_0101 : 5
ui8_one : 0000_0011 : 3
---------
ui8_two 0000_0001 : 1 .*/
printf( "%d bitor %d = %d\n",
ui8_one,
ui8_two,
( u_int8_t )( ui8_one bitor ui8_two ));
/* output :
3 bitor 1 = 1
ui8_one : 0000_0011 : 3
ui8_two : 0000_0001 : 1
---------
0000_0011 : 3 .*/
printf( "%d or_eq %d = %d\n",
ui8_two,
ui8_one,
( u_int8_t )( ui8_two or_eq ui8_one ));
/* output :
1 or_eq 3 = 3
ui8_two : 0000_0001 : 1
ui8_one : 0000_0011 : 3
---------
ui8_two 0000_0011 : 3 .*/
printf( "%d xor %d = %d\n",
ui8_one,
ui8_two,
( u_int8_t )( ui8_one xor ui8_two ));
/* output :
3 xor 3 = 0
ui8_one : 0000_0011 : 3
ui8_two : 0000_0011 : 3
---------
0000_0000 : 0 .*/
printf( "%d xor_eq %d = %d\n",
ui8_two,
ui8_one,
( u_int8_t )( ui8_two xor_eq ui8_one ));
/* output :
3 xor_eq 3 = 0
ui8_two : 0000_0011 : 3
ui8_one : 0000_0011 : 3
---------
ui8_two 0000_0000 : 0 .*/
printf( "compl %d = ~%d = %d\n",
ui8_two,
ui8_two,
( u_int8_t ) compl ui8_two );
/* output :
compl 0 = ~0 = 255
ui8_two 0000_0000 : 0
---------
1111_1111 : 255 .*/
printf( "compl %d = ~%d = %d\n",
ui8_one,
ui8_one,
( u_int8_t ) compl ui8_one );
/* output :
compl 3 = ~3 = 252
ui8_one 0000_0011 : 3
---------
1111_1100 : 252 */
return 0; }
char * boolToString( bool aBool ){
/* convert a boolean to a string .*/
return aBool == 1 ? "true" : "false";}