# Convert bool true/false to 1 and 0?

• (2 Pages)
• 1
• 2

## 22 Replies - 40667 Views - Last Post: 11 July 2010 - 10:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=180715&amp;s=07badb9bffcc14c44b29065ba0a46753&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 insanepenguin

Reputation: 7
• 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 1’s and 0’s, 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

Reputation: 1659
• 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;

```

### #3 insanepenguin

Reputation: 7
• 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

### #4 PsychoCoder

Reputation: 1659
• 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.

### #5 sarmanu

• D.I.C Lover

Reputation: 967
• 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.

### #6 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5104
• Posts: 9,283
• 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);
```

### #7 PsychoCoder

Reputation: 1659
• 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

### #8 insanepenguin

Reputation: 7
• 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));

```

### #9 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5104
• Posts: 9,283
• 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;
}
```

PsychoCoder, 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).

### #10 baavgai

• Dreaming Coder

Reputation: 7197
• Posts: 15,004
• 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

```

### #11 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5104
• Posts: 9,283
• 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

### #12 insanepenguin

Reputation: 7
• 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?

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!

### #13 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5104
• Posts: 9,283
• 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

### #14 baavgai

• Dreaming Coder

Reputation: 7197
• Posts: 15,004
• 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

### #15 Robin19

Reputation: 272
• Posts: 552
• 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);
```