Question:
java .what is bitwise operators?
John
2011-07-15 19:32:32 UTC
what means bitwise operator in java?bitwise OR(|) ,^

bitwise XOR?

for instance this is & bitwise and,also this is && and too so whats difference between them?
what for the bitwise operators used?
Five answers:
Vaibhav
2011-07-16 04:49:13 UTC
See & and && are different

if you provide logical operands to them then them will perform logical calculations on them

but if you provide numbers as operands then & will perform bitwise operations on them

suppose

A=5

B=6

A=101

B=110

A&B=100=4

A|B=111=7

A^B=011=3
husoski
2011-07-15 22:01:47 UTC
The bitwise operators (& | ^ ~) operate on integer-type values and treat them as bit arrays, with each bit interpreted as (0=false, 1=true). The logical versions (&& || ~) operate on boolean values and return a boolean result. (There isn't an operator specifically "logical XOR", but x != y is equivalent to x XOR y for boolean x and y.)



Bit manipulations (including the bitwise operations, plus shifts) are most often used for efficiently packing multiple binary fields into an int or long-sized package. One way to number bits ("little-endian") is to count from bit 0 as the least-significant bit. If you want to extract bits 3-5 from an int value m into another int k, you can use:



k = (m >> 3)&7



The reverse operation of storing the low 3 bits of k into bits 3-5 of m, without changing any other bits of m is:



m = (m & ~(7<<3)) | ((k^7) << 3);



Normally, you use symbolic constants instead of raw numbers. Note that 7 represents an int with 111 in the low 3 bits and 0 bits everywhere else.



Other uses arise in more serious data compression for both data and streaming media, encryption/decryption of binary data, and interpreting/converting binary data that doesn't fit one of the Java data types.



Plus there are a number of rare curiosities. For example:



static boolean isPowerOfTwo(int n) { return (n ^ -n) == n; }



...returns (true) if n is a power of 2 (1, 2, 4, 8, 16, ...) or (false) if not.
Jonh J
2011-07-15 20:55:44 UTC
Logical and/or (&& vs ||) are boolean operators that work on two boolean variables.



Bitwise and/or (& vs |) do something very similar, but they work on two integer variables. A bitwise operation will take two integers, and perform the logical operation on each pair of bits from the two numbers.



An example. 4 = 0100 in binary. 6 = 0110 in binary.



0100

0110

-------

0100



0100 in binary is 4. So 4 & 6 = 4.



You just take each pair of bits, perform the operation, and place the result in the same bit position in the output number.
MANOJ
2011-07-16 00:38:30 UTC
Bit Wise Operators: Bit operator in Java are use to manipulate variables at bit level. It is not recommended to use bit wise operatically in normal application programming because in makes code hard to understand. So, we are not going to discusses bit wise operators in details. In code below we shown how to use these operators.



http://javacodespot.blogspot.com/2011/04/java-programming-operators.html
?
2016-10-01 09:36:30 UTC
a = a hundred and one in binary b = 1001 in binary so & and promises you 0001 = a million in decimal | provides you 1101 = 13 ^ provides you 1100 it is 12 ~a and ~b are complicated via fact inner 2's compliment representation its 4 bytes trend could be ~a = 0xFFFA, ~b = 0xFFF6 ~a + 6 = 0x0000 so ~a = -6 ~b + 10 = 0x0000


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...