• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about bitwise OR

 
Greenhorn
Posts: 27
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(1000|4);
in this the output is :1004
System.out.println(010|4);
here Output is 12 ,
System.out.println(100|4);
here out put is 100 (4)

How the bitwise OR working ??

Thanks in advance

Kumar
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ,

Ans for First one is

the binary equivalent of 1000 is : 0000 0011 1110 1000
the binary equivalent of 4 is :0000 0000 0000 0100
--------------------
then result of (1000|4) is 0000 0011 1110 1100 is (1004)


Ans for Second one is

010 is Octal form which value is =8
i.e 1000 | 0100 =1100(12)

now u can found the ans for Third one

ok
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and remember everybody - this is a 1.4 topic, but NOT a tiger topic
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The | operator is a normal OR.
So

0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
1 | 1 = 1


Also it's nice to know how the ^ XOR works:

0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0


Transform all numbers in binairy numbers.
Apply the operators, and calculate it back.
(decimal, octal, hexadecimal)
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Msreddy,

<<
System.out.println(010|4);
here Output is 12 ,
>>

U have converted 010 to octal. What is the reason of assuming it as Octal ? Why can't we think it's '010' is 2 in binary ?

Cheers,

-Biswa
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an integer literal starts with 0, the compiler takes it as an octal value. Similarly, if a literal starts with 0x, the compiler takes it as a hexidecimal value. There is no way to specify binary literals that I know of. However, it is easy (and usually more convenient) to represent integers in octal or hexidecimal if you are manipulating the values at the bit level.

Layne
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seetharamireddy Mittala ,

I don't understand the third answer.
How come it is possible that the output of the following line :
System.out.println(100|4);

is 100 (4).

please clarify the doubt.


Thanks,
SKD
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As explained 1 | 1 is 1 ie 1 'OR' with anything is the always 1.

100 => binary equiv => 1100100 ( See 100 at the end of it )
4 => binary equiv => 100 ie 0000100
Now :
1100100 | 0000100 ie 1100100 ie nothing but 100 because 'hundred' is having a binay '1' exactly at the same position of decimal '4' ie 3rd bit is ON.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
100 in binary is 01100100. OR this with 4 i.e. 00000100 in binary
Remember, 1 | 1 is 1

regards,
vijay.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic