View Full Version : Simple Java Question " | "

UndefinedMind

October 27th, 2009, 04:49 AM

Simple code:

int f = 48, g = 16;

System.out.print( f | g );

The answer is 48, but I have no clue what the '|' operation does. Scrub question, but I've already googled with no success.

Reiger

October 27th, 2009, 05:14 AM

It is the ‘bitwise or’. In order to see what it does you must first convert the operands (48 and 16 in your case) to binary:

110000 = 48

010000 = 16

------------ |

110000 = 48

Another example:

110000 = 48

010011 = 19

------------ |

110011 = 51

UndefinedMind

October 27th, 2009, 05:26 AM

So, if that column contains a 1, the answer has a 1 in that spot, with a 0 being only if both columns have a 0?

NovaAesa

October 27th, 2009, 05:27 AM

So, if that column contains a 1, the answer has a 1 in that spot, with a 0 being only if both columns have a 0?

Correct. I have never used it though. It might be useful when trying to optimize something maybe?

UndefinedMind

October 27th, 2009, 05:39 AM

I understand now, thank you both.

This was a multiple choice question, it didn't give an example, just that bit of code.

Bitwise operations are necessary for much low-level programming, such as writing device drivers, low-level graphics, communications protocol packet assembly and decoding.

Although machines often have efficient built-in instructions for performing arithmetic and logical operations, in fact all these operations can be performed just by combining the bitwise operators and zero-testing in various ways.

For example, here is a pseudocode (http://en.wikipedia.org/wiki/Pseudocode) example showing how to multiply two arbitrary integers a and b (a less than b) using only bitshifts and addition:

c := 0

while b != 0

if (b and 1) != 0

c := c + a

shift a left by one

shift b right by one

return c

Reiger

October 27th, 2009, 05:43 AM

This kind of thing (bit manipulation) is typically useful for specifying cumulative bitmasks (i.e.: function(params, A_FLAG | ANOTHER_FLAG) ) as well as constructing values in binary file formats.

Bit-wise & for instance is very useful when converting unsigned bytes to integers because casting the byte value 0xFF to (signed) integer results in 0xFFFFFFFF (-1) -- and you want 0x000000FF (255). The bitwise & operation can fix that easily: int value = ((int) byteValue) & 0xFF.

Bit shifts combined with bit-wise or is very useful when packing multiple byte values into an int; furthermore, bit shifts occur in closed form solutions of the ‘Josephus problem’.

Arndt

October 27th, 2009, 10:36 AM

Simple code:

int f = 48, g = 16;

System.out.print( f | g );

The answer is 48, but I have no clue what the '|' operation does. Scrub question, but I've already googled with no success.

Try googling for "java operator".

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.