• Post Reply Bookmark Topic Watch Topic
  • New Topic

combination of shift & bitwise operators in java  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yeah, 0x3ff. I was doing it from my head, and I guess an extra 1 slipped in :P
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!