Help for this.

Java programming topics
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Thu Aug 05, 2010 5:55 pm

Yes brother I got many ideas from this.thanks for your help.but I have some questions about arrays.I will ask them in a new topic.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Fri Aug 06, 2010 10:56 pm

Code: Select all

char c='C'; // note that ASCII value of C is 67
int num1 = 1, num2 = 2, num3 = 3;
float x = 0.5f, y = 2.5f;

System.out.println((x > y) ^ (num1 != num2));
can you please tell me friend...in this code what"^" means..?????

please explain what happen in this line condition..(x > y) ^ (num1 != num2)
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Fri Aug 06, 2010 11:02 pm

Code: Select all

char c='C'; // note that ASCII value of C is 67
int num1 = 1, num2 = 2, num3 = 3;
float x = 0.5f, y = 2.5f;

System.out.println(num1 | num2);
I got this code from my friend about bit wise operators.

It is little hard to get in to my mind.can you please tell me about it & about this bit wise operators..

please explain what happen in this line System.out.println(num1 | num2); why it give us out put as 3?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Sat Aug 07, 2010 12:33 am

G-sparkZ wrote:

Code: Select all

char c='C'; // note that ASCII value of C is 67
int num1 = 1, num2 = 2, num3 = 3;
float x = 0.5f, y = 2.5f;

System.out.println((x > y) ^ (num1 != num2));
can you please tell me friend...in this code what"^" means..?????

please explain what happen in this line condition..(x > y) ^ (num1 != num2)
^ is the bitwise XOR operator.

The result of (x > y) is either 0 or 1. If x > y, the output of (x > y) is 1.
If x <= y {that is NOT (x > y) }, the result of (x > y) is 0.

So you now understand that (x > y) outputs either 0 or 1.

Lets take the other part (num1 != num2).
This of cause outputs either 0 or 1. If num1 not equals to num2, it outputs 1. Else (that is num1 == num2) it outputs 0.

Say (x > y) is A and (num1 != num2) is B. Then we have A ^ B.

The truth table of XOR is below.
XYResult
000
011
101
110
Now lets see what happens.

x = 0.5f, y = 2.5f, so (x > y) is false (0)
num1 = 1, num2 = 2, so (num1 != num2) is true (1)

0 XOR 1 = 1
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Sat Aug 07, 2010 12:50 am

G-sparkZ wrote:

Code: Select all

char c='C'; // note that ASCII value of C is 67
int num1 = 1, num2 = 2, num3 = 3;
float x = 0.5f, y = 2.5f;

System.out.println(num1 | num2);
I got this code from my friend about bit wise operators.

It is little hard to get in to my mind.can you please tell me about it & about this bit wise operators..

please explain what happen in this line System.out.println(num1 | num2); why it give us out put as 3?
You need to learn boolean algebra (very easy) about AND, OR, XOR, NOT, NAND, NOR operators. Read this one to understand the truth table for these operations. http://en.wikipedia.org/wiki/Logic_gate#Symbols

The pipe symbol (|) is used to denote bitwise OR operation.

The truth table for OR operation is as below.
XYResult
000
011
101
111
Say you need to find the answer for 57 | 23

Lets convert 57(dec) to binary. It is 111001 (bin). Then convert 23(dec) and it is 10111 (bin). (dec) for decimal and (bin) for binary. Write both these down by maintaining positions. I used to do two things here. Write in groups of 4 bits together (as b-bits represent one char of HEX, forget about this if you don't know that for now). Fill zeros until the number is a group of 4 bits.

0011 1001 (bin)
0001 0111 (bin) OR
------------------------

Now lets apply the truth table for each bit position (this is called bit wise operation).

0011 1111 (bin) = 63 (dec).

Not lets go to your example.

Here num1 = 1, num2 = 2

Convert these to binary.

num1 = 0001 (bin)
num2 = 0010 (bin)

num1 OR num2 is denoted by ( num1 | num2) in Java

0001 (bin)
0010 (bin) OR
------------
0011 (bin) = 3 (dec)
========
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Sat Aug 07, 2010 3:43 pm

^ is the bitwise XOR operator.

The result of (x > y) is either 0 or 1. If x > y, the output of (x > y) is 1.
If x <= y {that is NOT (x > y) }, the result of (x > y) is 0.

So you now understand that (x > y) outputs either 0 or 1.

Lets take the other part (num1 != num2).
This of cause outputs either 0 or 1. If num1 not equals to num2, it outputs 1. Else (that is num1 == num2) it outputs 0.

Say (x > y) is A and (num1 != num2) is B. Then we have A ^ B.

The truth table of XOR is below.
yeah bro i got the idea.Thank you for the help.I have little knowledge about Boolean algebra from physics.I had some lessons in AL physics subject.
The main problem is JAVA symbols are new to me.so i think the problem with the symbol knowledge in me.I am keep studding on that.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Sat Aug 07, 2010 3:52 pm

thank you for the wiki link you gave me.some fact are new to me on that page & some were in my knowledge.

I learn from you explanation you gave me about (1|2)..you are a good teacher NEO bro.

we can use 4digits 1-->15.....the biggest number that we can represent by 4 digits is 15..right? by 1111(binary)
If we need to convert the number over 15 we have to get another 4 digits code like this. 0010 0011...
(please tell me if I am wrong..)
num1 OR num2 is denoted by ( num1 | num2) in Java

0001 (bin)
0010 (bin) OR
------------
0011 (bin) = 3 (dec)
========
I got the idea from this example.

Thank you again...
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help for this.

Post by Neo » Sun Aug 08, 2010 12:35 am

G-sparkZ wrote:we can use 4digits 1-->15.....the biggest number that we can represent by 4 digits is 15..right? by 1111(binary)
If we need to convert the number over 15 we have to get another 4 digits code like this. 0010 0011...
(please tell me if I am wrong..)
Not quite clear. However the point is since 4 binary digits represent numbers from 0 to 15 (as you said too), we can easily convert a binary number to Hex.

Say we have a 8 digit binary number 1011 1101 (bin) and we need to convert it to Hex (base 16). Then we convert 1011 (bin) to Hex and write it as the at left side. 1011 (bin) = 11 (dec) = B (hex). Then we convert 1101 (bin). It is 13 (dec) and D (hex). So the conversion of the binary number to hex is BD (hex). Hex is usually written as 0xBD.

Hope you get the idea.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: Help for this.

Post by Face » Tue Aug 10, 2010 8:32 pm

yeah bro..thanks for the help..now I am with that calculator problem....
Post Reply

Return to “Java Programming”