Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a Conditional Function Using Bitwise Operators and !

Status
Not open for further replies.

SSJ2Joseph

Programmer
Sep 12, 2002
7
0
0
US
Hey all,
I am VERY new to C and I'm not completely comfortable with the operators. One of the books I've been using to learn C asks that I use the bitwise operators (~ & ^ | + >> <<) and the ! operator to define a conditional function : int conditional(int x, int y, int z). The function wants me to return y if x is true and z if it is false. I can't use any control statements (if, for, etc.) I can declare variables to store values. I am stumped and would appreciate it if someone told me how this is possible. Thanks.
 
You can read

a || b

as &quot;if (not a) execute b&quot;

a && b

as &quot;if (a) execute b&quot;

Left to right operation is guaranteed in && and ||.

Armed with this you can have

x && result = y;
x || result = z;
return result;

C is an expression language: i.e. any expression is a legal statement.
 
Thanks, but I can't use && or ||; I can use & and |. Maybe an example of what this function is supposed to do will help. I should be able to enter the expression : conditional(2,4,5); and get an answer of 4.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top