Convert bool true/false to 1 and 0?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 11984 Views - Last Post: 11 July 2010 - 10:36 PM Rate Topic: -----

#1 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 07:21 AM

Hello everyone,

I'm a beginner learning C# using "C # 3.0 A beginners guide" - Herbert Schildt

One of the exercises is to make a 'truth' table for the Logical Operations:

After doing this you are asked to:

Quote

On your own, try modifying the program so that it uses and displays 1s and 0s, rather than true and false.


I guess there are a few ways of doing this but so far I'm only on the basics i.e data types, variables, loops etc - I tried doing some if statements but it didn't work.

Any help appreciated!

using System;

class LogicalOpTable
{
    static void Main()
    {
        bool p, q;
       

        Console.WriteLine("P\tQ\tAND\tOR\tXOR\tNOT");

        p = true; q = true;

        Console.Write(p + "\t" + q + "\t");
        Console.Write((p & q) + "\t" + (p | q) + "\t");
        Console.WriteLine((p ^ q) + "\t" + (!p));

        p = true; q = false;
        Console.Write(p + "\t" + q + "\t");
        Console.Write((p & q) + "\t" + (p | q) + "\t");
        Console.WriteLine((p ^ q) + "\t" + (!p));

        p = false; q = true;
        Console.Write(p + "\t" + q + "\t");
        Console.Write((p & q) + "\t" + (p | q) + "\t");
        Console.WriteLine((p ^ q) + "\t" + (!p));

        p = false; q = false;
        Console.Write(p + "\t" + q + "\t");
        Console.Write((p & q) + "\t" + (p | q) + "\t");
        Console.WriteLine((p ^ q) + "\t" + (!p));
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Convert bool true/false to 1 and 0?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 07:30 AM

You could always do something like this

int i = (p == true) ? 1 : 0;
int x = (q == true) ? 1 : 0;


Was This Post Helpful? 1
  • +
  • -

#3 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 07:44 AM

Hi PsychoCoder,

Would that replace the p = true; q = true; code or go somewhere else?

   p = true; q = true;

        Console.Write(p + "\t" + q + "\t");
        Console.Write((p & q) + "\t" + (p | q) + "\t");
        Console.WriteLine((p ^ q) + "\t" + (!p));


Thanks
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 07:47 AM

That's an example of using a ternary operator to convert your boolean values to integer values. That would not replace creating & setting p and q but would be used for printing out values (either 0 or 1) based on the values of your 2 boolean variables.
Was This Post Helpful? 1
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 07:48 AM

No. p and q will be displayed as true/false anyway.
int i = (p == true) ? 1 : 0;


Basically, i will be 1 or 0, depending on p value. If p is true, then i becomes 1. False otherwise.
Same thing goes here:
int x = (q == true) ? 1 : 0;


So, you have to output i instead of p, and x instead of q.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4439
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:08 AM

One of my pet peeves: testing if a boolean value equals true or false by comparing it to true or false. To shorten what Psycho posted:
int i = p ? 1 : 0;


You can even do your tests like this. Parenthesis added for clarity, but not required:
int x = (p | q) ? 1 : 0;


But as much as I love the ternary operator, you could also use the Convert.ToInt32 method:
int i = Convert.ToInt32(p);

Was This Post Helpful? 2
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:10 AM

I also hate checking for true but this time I did it for the sake of the OP, who's just learning and I wanted him to be able to see what I was checking. If it were for me I would never check for true
Was This Post Helpful? 1
  • +
  • -

#8 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:10 AM

Ok, I used it for the values that would be printed out and get:

Quote

Error 1 Operator '!' cannot be applied to operand of type 'int' C:\Users\Michael\Documents\Visual Studio 2010\Projects\LogOpsTable\LogOpsTable\CodeFile1.cs 18 45 LogOpsTable


I'm probably doing something wrong! sorry :(

using System;

class LogicalOpTable
{
    static void Main()
    {
        bool p, q;
        
 
        Console.WriteLine("P\tQ\tAND\tOR\tXOR\tNOT");

        p = true; q = true;

        int i = (p == true) ? 1 : 0;
        int x = (q == true) ? 1 : 0;

        Console.Write(i + "\t" + x + "\t");
        Console.Write((i & x) + "\t" + (i | x) + "\t");
        Console.WriteLine((i ^ x) + "\t" + (!i));


Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4439
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:17 AM

The integer should represent your result, not be one of the operands you use for your tests. Something like this:
static void Main(string[] args)
{
    bool p = true, q = false;
    int and = btoi(p & q);
    int or = btoi(p | q);
    int xor = btoi(p ^ q);
    int not = btoi(!p);
    //do your display logic with the results     
}

static int btoi(bool B)/>
{
    return b ? 1 : 0;
}


View PostPsychoCoder, on 08 July 2010 - 02:10 PM, said:

I also hate checking for true but this time I did it for the sake of the OP, who's just learning and I wanted him to be able to see what I was checking. If it were for me I would never check for true


Figured something like that. This way, we have our cake and eat it too. OP knows how the operator works, and knows how we don't like doing (b == true).
Was This Post Helpful? 1
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:33 AM

You want a truth table, but you've only done one line. Learn to use methods, they help a lot.

Perhaps something like this:
using System;

class LogicalOpTable {
	static void ShowLine(bool p, bool q) {
		int np = p ? 1 : 0;
		int nq = q ? 1 : 0;
		Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", 
			(np), // "P"
			(nq), // "Q"
			(np & nq), // "AND"
			(np | nq), // "OR"
			(np ^ nq), // "XOR"
			(!np) // "NOT" // this one doesn't make much sense
			);
	}

	static void Main() {
		Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", "P","Q","AND","OR","XOR","NOT");
		ShowLine(true, true);
		ShowLine(true, false);
		// two more, you type em


Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4439
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:40 AM

baavgai, that won't compile. You can't apply not (!) to an int.

By the way, insanepenguin, you are using bitwise operators. Those can be applied to integers, but not necessarily with the result you would expect. In this case it will work, because your ints are actually 0 and 1.

This post has been edited by insertAlias: 08 July 2010 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

#12 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:46 AM

Thanks for all the suggestions guys, from the books perspective I think I'm supposed to do this with what I have already learned and the only thing I can think of (that I've learned so far and might apply to this problem) is an if statement? :D

Could someone write a code example without it being shortened?

I haven't started using the more efficient code like below yet
int i = (p == true) ? 1 : 0;
int x = (q == true) ? 1 : 0;

Thanks for putting up my with newbness!
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4439
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:49 AM

int i = p ? 1 : 0;

Is the equivalent of:
int i;
if(p)
  i = 1;
else
  i = 0;


So if you need to do if statements, do that. Take a look at this reply and convert the btoi method to use the if statement.

This post has been edited by insertAlias: 08 July 2010 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 08:49 AM

These are the same:
int i = (p == true) ? 1 : 0;

int i = (p) ? 1 : 0;

int i;
if (p == true) {
   i = 1;
} else {
   i = 0;
}

int i;
if (p) { i = 1; } else { i = 0; }

int i = 0;
if (p) { i = 1; }


This post has been edited by baavgai: 08 July 2010 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#15 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • Posts: 548
  • Joined: 07-July 10

Re: Convert bool true/false to 1 and 0?

Posted 08 July 2010 - 12:19 PM

If you wanted to do it on the fly:
            Console.Write("{0} \t{1} \t", p ? 1 : 0, q ? 1 : 0);
            Console.Write("{0} \t{1} \t", (p && q) ? 1 : 0, (p || q) ? 1 : 0);
            Console.WriteLine("{0} \t {1}", (p ^ q) ? 1 : 0, (!p && !q) ? 1 : 0);

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2