11 Replies - 607 Views - Last Post: 06 March 2013 - 06:05 AM Rate Topic: -----

#1 hananeWeb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-March 13

how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 06:48 PM

This code generates inputs of a truth table. My question is how to generate the outputs of an AND gate of the same truth table???
int n=3;
		for (int i = 0 ; i != (1<<n) ; i++) {
		    String s = Integer.toBinaryString(i);
		    while (s.length() != 3) {
		        s = '0'+s;
		    }
		    System.out.println(s);
		}


Is This A Good Question/Topic? 0
  • +

Replies To: how to produce outputs for an adder gate for tthe truth table?

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 07:41 PM

why complicating your life like that "!= (1<<n)" ?
Was This Post Helpful? 0
  • +
  • -

#3 hananeWeb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-March 13

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 07:47 PM

!=(1<<n) will shift the bit to the right with n cases...
but at least it works
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 07:52 PM

so if n == 3 what don't you simply write 8
you badly hardcoded 3 in the loop anyhow
Was This Post Helpful? 0
  • +
  • -

#5 hananeWeb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-March 13

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 08:06 PM

Because I am using the code in a methode:
public static void printTruthTable(int n)

Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 08:09 PM

It does not print a thruth table, it print the number 0 to n in binary
and when n will be > 0xF your loop inside to left fill with '0' won't work
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 08:23 PM

I think I understand what you want

	private static void print(int n) {
		
		for(int i = 0; i <= n; i++) {
			for(int j = 0; j <= n; ++j) {
				int or = i | j;
				int and = i & j;
				int xor = i ^ j;
				System.out.printf("%3x %3x %3x %3x %3x\n", i, j, or, and, xor);
			}
		}
	}


Simply write a method

static String toBin(int n, int size) {

that returns the binary representation of n in size bits
Was This Post Helpful? 0
  • +
  • -

#8 hananeWeb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-March 13

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 08:59 PM

your code prints this:
0 0 0 0 0
0 1 1 0 1
0 2 2 0 2
0 3 3 0 3
1 0 1 0 1
1 1 1 1 0
1 2 3 0 3
1 3 3 1 2
2 0 2 0 2
2 1 3 0 3
2 2 2 2 0
2 3 3 2 1
3 0 3 0 3
3 1 3 1 2
3 2 3 2 1
3 3 3 3 0
this is not what i need!!!!
my code above generates inputs of a truth table(binary representation),I need help to generates the outputs.
my inputs are:
000
001
010
011
100
101
110
111
I need to put this into a two dimensional array, then use AND gate ...like o && 0 && 0 is 0
and so one.
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 09:19 PM

You think I will do everything ?

I told you in my previous poost

Quote

Simply write a method

static String toBin(int n, int size) {


I fixed and coded your logic problem
Now your job: just format the output, this is a basic really junior job that any beginner should be able to achieve

You did it in your first post for one column
Now you just have to do it for 5 columns... thus the good idea of writing a method for it

This post has been edited by pbl: 05 March 2013 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 09:34 PM

View PosthananeWeb, on 05 March 2013 - 10:59 PM, said:

your code prints this:
0 0 0 0 0
0 1 1 0 1
0 2 2 0 2
0 3 3 0 3
1 0 1 0 1
1 1 1 1 0
1 2 3 0 3
1 3 3 1 2
2 0 2 0 2
2 1 3 0 3
2 2 2 2 0
2 3 3 2 1
3 0 3 0 3
3 1 3 1 2
3 2 3 2 1
3 3 3 3 0
this is not what i need!!!!

I should give you a -1 to be so impertinent

Quote

my code above generates inputs of a truth table(binary representation),I need help to generates the outputs.
my inputs are:
000
001
010
011
100
101
110
111
I need to put this into a two dimensional array, then use AND gate ...like o && 0 && 0 is 0
and so one.

this is EXACTLY what I have produced.. printing in hex rather than in binary... you can Google hexadecimal if you don't know that representation
Was This Post Helpful? 0
  • +
  • -

#11 raghav.naganathan   User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 05 March 2013 - 09:44 PM

Well, you could always use the built in method Integer.toBinaryString() if you don't wanna create a user defined method.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#12 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to produce outputs for an adder gate for tthe truth table?

Posted 06 March 2013 - 06:05 AM

I think that Java 7 has a %b formatter for the String.format() method so, if it is the case

private static void print(int n) {
	
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j <= n; ++j) {
			int or = i | j;
			int and = i & j;
			int xor = i ^ j;
			System.out.printf("%03b %03b %03b %03b %03b\n", i, j, or, and, xor);
		}
	}
}


but I may be completly wrong. %b may be for boolean and Boolean. It could be just the 0b10101 that was added to JRE 7

In that case use [meber=raghav.naganathan]'s trick

String format(int n, int size) {
   String str = Integer.toBinaryString(n);
   while(str.length() < size)
     str = "0" + str;
   return str;
}


This post has been edited by pbl: 06 March 2013 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1