• 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

Bitwise and shift operators

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the use of Bitwise and shift operators in real world?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The obvious answer is: to manipulate bits in an integer directly. You should not need that very often in Java, as these are pretty low-level operations. One of reasonable usages would be to interpret data in a binary format which employs bitfields.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please search these fora for “bitwise”; I seem to have written quite a lot about bitwise operators myself. There is a little about them in the Java Tutorials.
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bitwise operators can be used with binary numbers(0 and 1).Bitwise Operators are:
1. Bitwise or(|)
2. Bitwise and(&)
3. Bitwise complement(~)
4. Bitwise Xor(^)
Bitwise or: The decimal number should be converted into a binary number. Bitwise operators operate only with binary bits. Bitwise or(|) gives 1 if either one of the inputs is 1, otherwise 0.
ex: a=2 =0010
     b=3 =0011
a|b=-----------------
              0011=3
-----------------------
Bitwise and: Bitwise and(&) performs an operation if both bits are 1,it gives 1 otherwise 0.
ex: a=2 =0010
     b=3 =0011
a&b=-----------------
              0010=2
-----------------------
Bitwise complement: It returns the one’s complement representation of the input value, i.e, with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.
For example,
a = 5 = 0101

Bitwise Complement Operation of 5

~ 0101
-------------
 1010  = 10
Bitwise XOR: if  the bits are different, it gives 1, else it gives 0.
for ex:
a = 5 = 0101
b = 7 = 0111

Bitwise XOR Operation of 5 and 7
 0101
^ 0111
________
 0010  = 2
         
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bitwise Operators are the operators that are used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
Bitwise operators are,
AND : op1 & op2 -- The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.
OR : op1 | op2 -- The OR operator compares two bits and returns 1 if either or both of the bits are 1 and it gives 0 if both bits are 0.
EXCLUSIVE-OR : op1^ op2 -- The EXCLUSIVE-OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.
COMPLEMENT(~) operator : ~op1 -- The COMPLEMENT operator is used to invert all of the bits of the operand.
SHIFT RIGHT operator: op1 >> op2 -- The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half.
SHIFT LEFT operator: op1 << op2 -- The SHIFT LEFT operator moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. Each move to the left effectively multiplies op1 by 2.
some real world use cases of the bitwise operators are,
Bit fields (flags),Checking for Odd and Even Numbers,and also In microcontroller applications, you can utilize bitwise to switch between ports,
Left Shift is used to Muliply by any power of two and Right bit shifting to divide by any power of two.
For example x= x* 2 can also be written as x<<1 or x= x*8 can be written as x<<3. Similarly x = x / 2; is x>>1 and so on...
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bitwise operations occur in a surprising number of places. For example, the low-level implementation of multiplication consists of add-and-shift sequences.

They are also extremely important in both encryption and error checking-and-correcting systems where they facilitate the processing of binary polynomials.

Here's the polynomial expression used for CRC-32 computation (32-bit Cyclic Redundancy check)

x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1

Functionally, you can consider the input to this function (x) to be a very large binary number (say 1024 bits). Each exponent listed in this formula corresponds to the number of bits to shift the original number. This may look intimidating, but disk drives and network cards can do this sort of stuff on a streaming basis, so it can be done very fast.
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Godia:

Welcome to the Ranch!!

The original poster has been away from keyboard for 7 years, so they are unlikely to see the post, but it is still interesting to discuss this.

Regarding part of your answer shown below:

Godia Bhavana wrote:Bitwise Operators are the operators that are used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
Bitwise operators are,
...

SHIFT RIGHT operator: op1 >> op2 -- The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. Each move to the right effectively divides op1 in half.



This is not true in Java.  It is true for unsigned types in C/C++, but not true for negative values of integral types.

Focusing on Java, two things to note:
1. if you want to shift 0's in, you use >>> operator, which does not exist in C/C++.  This will not effectively divide op1 in half if op1 is negative:
Example:
jshell> -20 >>> 3
$79 ==> 536870909

2. If you want to keep the sign as it is, you use the >> operator, this will effectively divide op1 in half regardless of sign.  However, if op1 is negative, it will be shifting in 1 bits, not 0's to achieve this:
jshell> Integer.toBinaryString(-20>>3)
$81 ==> "11111111111111111111111111111101"


I lied, there is a third thing important to know about bit-shifting in Java, regardless of the sign of op1: op2 is interpreted modulo 32 for int and modulo 64 for long, so that whereas C/C++/Python will give us these same results:
>>> 404 >> 32
0
>>> 808 >> 64
0
>>> 404 >> 33
0
>>> 808 >> 65
0


Our friend Java instead gives us these results:
jshell> 404 >> 32
$82 ==> 404

jshell> 808L >> 64
$83 ==> 808

jshell> 404 >> 33
$1 ==> 202

jshell> 808L >> 65
$3 ==> 404


Whether this would matter to our OP will never be known unless they return, but it is important to remember these if you are porting code from Python/C/C++/C# to Java or vice versa, so I repeat it here.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are trying to port code, I think it is better to write the formula out in ordinary algebra and use that to write code in the new language. The subtle differences between meanings of keywords and operators in different languages make porting code dangerously error‑prone.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I agree, there are things that look so familiar that many will fall for the false cognates.

To some extent, I am reminded of everyone's hatred of UML.

If we all agree that analysis and design should come before diving into coding, don't those artifacts of those stages necessarily need to be made out of something other than code?

Is UML so bad for that?

I was thinking that as I have been studying a book that does use UML notation for the artifacts of the processes, because it has to go SOMEWHERE.

They repeatedly remind people that the processes and the decisions are more important than the UML, which by itself is worthless.

So yeah, porting can be tricky, but what intermediate would you elsewise use?  Pseudo-code?  UML?

I am really not sure.  In the past I've gone from code-to-code, which can work if you know both languages quite well, otherwise, not so great.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:
So yeah, porting can be tricky, but what intermediate would you elsewise use?  Pseudo-code?  UML?



I would use anything that works. Generally on the back of an envelope. I might even use CRC cards, which is a lot like UML class diagrams.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . look so familiar that many will fall for the false cognates.

I have seen it happen myself.

. . . hatred of UML. . . . more important than the UML, which by itself is worthless. . . .

I haven't written UML for fifteen years, but I thought it only went down to the level of methods, so the UML doesn't record the details of any algorithms.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UML does have a logic flow component. It's somewhat different than flowcharts, though.

Most UML I've seen has been actor diagrams (I knew a major company who wasted way too much time on those) and class diagrams.

The most useful part of UML for me has been Swim Lane diagrams, since it can be used to chart interactions between clients and servers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic