• 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

combination of shift & bitwise operators in java

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


What is the purpose of performing both bitwise and shift operations in combinations?e.g. (5 & (1<<9))
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prakash, welcome to CodeRanch.

Using shifts and masks is usually done to extract a value out of a larger sequence of bits. For instance, an int can hold 32 bits of information. Some data formats will stuff multiple variables into those 32 bits to save space.

Let's say we use an int to store XYZ coordinates, and x is stored in the least significant 10 bits, y in the next 10, and z in the next, leaving 2 unused bits. We can extract these values from the variable like this:
The expression you posted doesn't really make much sense, because it evaluates to a constant (0). You might as well just use the constant directly. The & operator is also rarely used in conjunction with the << operator. It's much more useful in combination with the >> operator.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prakash Venugopal wrote:What is the purpose of performing both bitwise and shift operations in combinations?e.g. (5 & (1<<9))


Stephan already gave you a nice explanation, but here's a practical example:I'll leave you to figure out how it works .

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I'm mistaken, but that can't work, can it?

Did you mean to use the logical shift, as opposed to arithmetic shift?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Did you mean to use the logical shift, as opposed to arithmetic shift?


If you mean the 0-fill shift, then yes.

Winston
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sure it’s 0x07ff to get 10 bits? Isn’t it 0x03ff?

You need to compare the precedences and associativity of operators when you use them. Everybody remembers * is higher than +, but can you remember the precedences for << >> and &? Try here. Note the loose use of "logical" for && and ||. There is a more formal description of operators, starting with the highest precedences, here.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...but can you remember the precedences for << >> and &? Try here...


Thanks Campbell; I'm always losing that darn page and have to look it up every time. Bookmarked now.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yeah, 0x3ff. I was doing it from my head, and I guess an extra 1 slipped in :P
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic