Home static Java Programming [Archive] - Bitwise operator: (010|4)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Bitwise operator: (010|4)
 This topic has 9 replies on 1 page.

Posts:640
Registered: 5/22/02
Bitwise operator: (010|4)
Jun 29, 2004 7:06 AM

Executing the following code results in "12". I do not understand why?
``public class Test {    public static void main(String[] args) {        System.out.println(010|4);    }}``

From what I understand, the answer should be 6. What is wrong with my below logic?

010 = 2
100 = 4

110 = 6

Thanks!

Posts:342
Registered: 5/22/98
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:11 AM (reply 1 of 9)

You are ORing 10 and 4, not 2 and 4.

Graeme.

Posts:7,258
Registered: 1/31/02
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:12 AM (reply 2 of 9)

010 octal is 8 decimal - the leading 0 causes the compiler to treat your number as base 8.

Good Luck

Lee

Posts:7,258
Registered: 1/31/02
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:13 AM (reply 3 of 9)

You are ORing 10 and 4, not 2 and 4.

Graeme.

If that were the case, he would get 14, not 12...

Posts:640
Registered: 5/22/02
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:18 AM (reply 4 of 9)

Thanks for the reply! Can you further clarify?

So if it starts with a 0, then it is octal. Does that mean that if it starts with a 1 its binary? Is there any documentation on this that I could read?

Thanks again for the help!

Posts:342
Registered: 5/22/98
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:27 AM (reply 5 of 9)

If that were the case, he would get 14, not 12...

This still means that your expected output is wrong tho as you are ORing 8 and 2.

Check out http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#228794.

Graeme

Posts:7,258
Registered: 1/31/02
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:30 AM (reply 6 of 9)

You can check out the JLS - go here:

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html

And look at section 3.10.1

There's no way to explicitly enter a base 2 digit (that I know of) - you can specify it as base 16 by prepending with a 0x, or octal by prepending with a 0. You could use the Integer.parseInt(value, radix) method to do this by passing in the binary digit string and a radix of 2, as
``int value = Integer.parseInt("010", 2)``
I hope that helped

Lee

Posts:7,258
Registered: 1/31/02
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:31 AM (reply 7 of 9)

If that were the case, he would get 14, not 12...

octal. DOH.

This still means that your expected output is wrong
tho as you are ORing 8 and 2.

Actually, he is ORing 8 and 4

:-)

Posts:640
Registered: 5/22/02
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:34 AM (reply 8 of 9)

That helped greatly. Thanks for the links!

Posts:342
Registered: 5/22/98
Re: Bitwise operator: (010|4)
Jun 29, 2004 7:52 AM (reply 9 of 9)

If that were the case, he would get 14, not 12...

octal. DOH.

This still means that your expected output is wrong
tho as you are ORing 8 and 2.

Actually, he is ORing 8 and 4

:-)

Wow, I'm really having a great day today aren't I?

Graeme

 This topic has 9 replies on 1 page.