Jump to content

How do you change boolean True/False to 1/0?

- - - - -

  • Please log in to reply
13 replies to this topic

#1
insanepenguin

insanepenguin

    Newbie

  • Members
  • Pip
  • 5 posts
I'm working through C# 3.0 Beginner's Guide and built a 'truth' table for the logical operators

using System;


class LogiOpsTable

{

    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));


    }

}


5. Compile and run the program. The following table is displayed:
P Q AND OR XOR NOT
true true true true false false
true false false true true false
false true false true true true
false false false false false true

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

Here is where I'm struggling you are meant to do this with what you have learned previously, which is basically data types, loops and if statements.

Any idea how to make the table display 1 - 0 instead of true - false?

thanks a lot!

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Hi,

Simplest of all ways to convert Boolean variable to Int32 is to use utility class Convert (from the System namespace). This class is used to convert values among base types. Note that Boolean False equals Int32 0 and True equals Int32 1.

Hence, your code would look like this (only inner part of the Main function):

bool p, q;


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


p = true; q = true;


Console.Write(Convert.ToInt32(p) + "\t" + Convert.ToInt32(q) + "\t");

Console.Write(Convert.ToInt32(p & q) + "\t" + Convert.ToInt32(p | q) + "\t");

Console.WriteLine(Convert.ToInt32(p ^ q) + "\t" + Convert.ToInt32(!p));


p = true; q = false;


Console.Write(Convert.ToInt32(p) + "\t" + Convert.ToInt32(q) + "\t");

Console.Write(Convert.ToInt32(p & q) + "\t" + Convert.ToInt32(p | q) + "\t");

Console.WriteLine(Convert.ToInt32(p ^ q) + "\t" + Convert.ToInt32(!p));


p = false; q = true;


Console.Write(Convert.ToInt32(p) + "\t" + Convert.ToInt32(q) + "\t");

Console.Write(Convert.ToInt32(p & q) + "\t" + Convert.ToInt32(p | q) + "\t");

Console.WriteLine(Convert.ToInt32(p ^ q) + "\t" + Convert.ToInt32(!p));


p = false; q = false;


Console.Write(Convert.ToInt32(p) + "\t" + Convert.ToInt32(q) + "\t");

Console.Write(Convert.ToInt32(p & q) + "\t" + Convert.ToInt32(p | q) + "\t");

Console.WriteLine(Convert.ToInt32(p ^ q) + "\t" + Convert.ToInt32(!p));



#3
insanepenguin

insanepenguin

    Newbie

  • Members
  • Pip
  • 5 posts
thanks zoranh!

I've been on other forums and got code that I couldn't understand, this was easy and explained well :D

Edited by insanepenguin, 09 July 2010 - 01:08 AM.


#4
vinyl

vinyl

    Newbie

  • Members
  • Pip
  • 4 posts
try this for "table" output >>

            Boolean p, q;


            String f = "{0,-4}{1,-4}{2,-4}{3,-4}{4,-4}{5,-4}";


            Console.WriteLine(String.Format(f, "P", "Q", "AND", "OR", "XOR", "NOT"));


            p = true; q = true;


            Console.WriteLine(String.Format(f, Convert.ToInt32(p), Convert.ToInt32(q), Convert.ToInt32((p & q)), Convert.ToInt32((p | q)), Convert.ToInt32((p ^ q)), Convert.ToInt32(!p)));


            p = true; q = false;


            Console.WriteLine(String.Format(f, Convert.ToInt32(p), Convert.ToInt32(q), Convert.ToInt32((p & q)), Convert.ToInt32((p | q)), Convert.ToInt32((p ^ q)), Convert.ToInt32(!p)));


            p = false; q = true;


            Console.WriteLine(String.Format(f, Convert.ToInt32(p), Convert.ToInt32(q), Convert.ToInt32((p & q)), Convert.ToInt32((p | q)), Convert.ToInt32((p ^ q)), Convert.ToInt32(!p)));


            p = false; q = false;


            Console.WriteLine(String.Format(f, Convert.ToInt32(p), Convert.ToInt32(q), Convert.ToInt32((p & q)), Convert.ToInt32((p | q)), Convert.ToInt32((p ^ q)), Convert.ToInt32(!p)));



#5
brightmatter

brightmatter

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
really? ([your boolean] ? 1 : 0) works just as well. perhaps i missed something.

#6
abzero

abzero

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 217 posts
you can also not use bools in this, and do the bitwise ops on ints...(i think)

#7
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

brightmatter said:

really? ([your boolean] ? 1 : 0) works just as well. perhaps i missed something.

Yes but propositions did not list ternary operator, as quoted below:

insanepenguin said:

Here is where I'm struggling you are meant to do this with what you have learned previously, which is basically data types, loops and if statements.

I have tried to make an educational reply.

Concerning bitwise operations, that is the most efficient solution but requires a bit more learning than expected by the beginner. The solution looks like this:


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


for (int p = 0; p < 2; p++)

    for (int q = 0; q < 2; q++)

        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", p, q, p & q, p | q, p ^ q, 1 - p);


Note that bitwise not operator (~) is not applicable in this case.

#8
insanepenguin

insanepenguin

    Newbie

  • Members
  • Pip
  • 5 posts
Yes, the ToInt32 method was easy to understand for a newbie like myself!

#9
brightmatter

brightmatter

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
While I agree, I also see benefit in new programmers using actual code written by them. To call a function from a library is a bit like saying, "you don't need to know how to do what is asked because someone else already created this code so just use theirs". I am well aware that yours is a real world response. It is practical and common in the work place. My only opinion is that what you were teaching was perhaps, skipping the point of the assignment. I could be wrong so please don't receive any of this as insult or judgement.

I also feel bitwise operations are esoteric in nature. They historically were very common because of severe limitation on memory but these problems have moved to small IO devices where tight memory management is necessary. In my opinion, if you are using bitwise operations, you are in fact making your code less readable, more prone to error through superfluous complexity and reducing maintainability. It is my thoughts that if you don't deed to do it, don't. I only have a few years in the industry of designing commercial code so I am by no means an expert.

You are correct, I forgot that a ternary is not a true If-Else. Thus I would instead use this. Technically the assignment did not allow "else" but I feel that it was implied.

if([your boolean] == true)
{
[your variable] = 1;
}
else
{
[your variable] = 0;
}

" == true" is superfluous but I get yelled at all the time for leaving it off. It goes back to the concept: explicit code is more readable than implicit code.

Don't sacrifice readability for succinctness and don't sacrifice efficiency for verbosity. Life is a balance and so is coding.

Edited by brightmatter, 09 July 2010 - 04:12 PM.
grammar error - incomplete thought


#10
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
actually speaking True,False are enumurators.They are values 0,1.So why dont you make a enumurator yourself & use it instead of depending on the c# frameworks True,False

#11
insanepenguin

insanepenguin

    Newbie

  • Members
  • Pip
  • 5 posts
using System;

class LogiOpsTable
{
    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));

How would it work for this as p and q both = true?

I think the book does want me to use if or if-else because apart from loops and data types that all I've learned so far

thanks

#12
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

brightmatter said:

I also feel bitwise operations are esoteric in nature. They historically were very common because of severe limitation on memory but these problems have moved to small IO devices where tight memory management is necessary. In my opinion, if you are using bitwise operations, you are in fact making your code less readable, more prone to error through superfluous complexity and reducing maintainability. It is my thoughts that if you don't deed to do it, don't. I only have a few years in the industry of designing commercial code so I am by no means an expert.

This is true - don't use bitwise operators if not necessary.

One example where they are normally used are flag enumerations:


using System;


namespace ConsoleApplication

{

    class Program

    {


        [FlagsAttribute()]

        enum CarDevices

        {

            None = 0,

            Radio = 1,

            Light = 2,

            HeadLights = 4,

            PhoneCharger = 8,

            All = Radio | Light | HeadLights | PhoneCharger,

            Entertainment = Radio | Light | PhoneCharger

        }


        static void Main(string[] args)

        {

            

            CarDevices cd = CarDevices.None;


            cd = cd | CarDevices.PhoneCharger; // Switch on PhoneCharger

            Console.WriteLine(cd.ToString());

            cd = cd ^ CarDevices.Light;        // Toggle light

            Console.WriteLine(cd.ToString());

            cd = cd | CarDevices.Radio;        // Turn radio on

            Console.WriteLine(cd.ToString());

            cd = cd & (~CarDevices.Radio);     // Turn radio off

            Console.WriteLine(cd.ToString());


            bool isLightOn = (cd & CarDevices.Light) == CarDevices.Light;

            Console.WriteLine("Light is {0}.", isLightOn ? "ON" : "OFF");


        }

    }

}


Output of this code is this:

PhoneCharger

Light, PhoneCharger

Entertainment

Light, PhoneCharger

Light is ON.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users