• 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

Bit wise ,shift Operators practical usage

 
Ranch Hand
Posts: 49
Java ME PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Bit wise ,shift Operators ?
2.what Bit wise ,shift Operators are used for?
3.what is practical usage of them and when we should use them?

Please can anyone help me to understand this things?
thank you...
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Convert the values in a byte array to a string containing hexadecimal characters.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bitwise operators are normally not used for business logic, much more for low level (on bytes etc) operations. One place where they are used much is cryptography and (again, low level) communication protocols.

As for the toHexStrring method:



Seems a bit more readable to me. Note the & bitwise operator that converts the (possibly negative) signed byte value to a possitive int.

[EDIT] this is a bit unfair of course, since String.format() almost certainly will use shift operators to generate the 2 digit hexadecimal string similar to the code given in the post above
 
deca leni
Ranch Hand
Posts: 49
Java ME PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hm .. thank you all..
I'm waitng mere explanations...
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I need bitwise operators, I'm usually working with an embedded device or storage format, that uses very tight packing of data.

Imagine if you have several integer variables who's value will never exceed a certain limit, then you can pack them together tightly to save space.

An example of such an application I'm currently working with: In a particular resource index file format, there's a 4 byte field that encodes the index of the archive where a resource is located, as well as the index of the resource within that archive. The 12 most significant bits stand for the archive index, the next 6 bits stand for a special value. The 14 least significant bits stand for the resource index.

I can then retrieve all the values as follows:

The shift (>>) is used to 'cut off' the bits to the right of the required index. Note that "resource" doesn't need to be shifted, because it is located at the least-significant-bit of the value.
The mask (&) is used to 'zero out' the bits to the left of the required index. Note that "archive" requires a mask, even though it's located at the most-significant-bit, because the shift operator will fill the most significant bits with 1, instead of 0, if value happens to be negative. Here's what really happens:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a topic from earlier today, where Roger needed bitwise operators to solve a practical problem.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the & operator to dispense with division in this exampleIt gives a slight performance advantage over % 2, but there is a subtle difference in its behaviour. You might find out what the subtle difference is if you change that code by replacing & 1 by % 2.
You can use the & and | operators to create and use masks.
You can use the shift operators instead of multiplying or dividing by 2, 4, 8, etc. Try this old thread and see whether I have actually given you the correct link
reply
    Bookmark Topic Watch Topic
  • New Topic