Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to understand the sign "|="?  RSS feed

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,the following code snippet come from the ode of lucene2.3.
How to understand the sign "|="? Thanks a lot.
 
Ranch Hand
Posts: 2908
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operator is OR operator (" | ") ,

| (Bitwise OR) sets a bit to 1 if one or both of the corresponding bits in its operands are 1, and to 0 if both of the corresponding bits are 0. In other words, | returns one in all cases except where the corresponding bits of both operands are zero.(that means , both bits are ZERO , then it ZERO ) The resulting bit pattern is the "set" (1 or true) bits of any of the two operands. This property is used to "set" or "turn on" a "flag" (bit set to one) in your flags or options variable regardless of whether that flag was set previously or not. Multiple flag bits can be set if a combo MASK is defined.

// To set or turn on a flag bit(s)

flags = flags | MASK;

// or, more succinctly

flags |= MASK;

Regardz,
 
Sheriff
Posts: 21462
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll also have to understand binary a bit, and how integers are represented in binary.

Basically, an integer is a 32 bit number, with the most left bit indicating if the number is positive (0) or negative (1).

So let's take 7 | 26

07 = 0000 0000 0000 0000 0000 0000 0000 0111
26 = 0000 0000 0000 0000 0000 0000 0001 1010

The result is 31:
31 = 0000 0000 0000 0000 0000 0000 0001 1111


Now the most used case is as in Lucene, where every number you add represents a different bit.

0x01 = 0000 0000 0000 0000 0000 0000 0000 0001
0x02 = 0000 0000 0000 0000 0000 0000 0000 0010
0x04 = 0000 0000 0000 0000 0000 0000 0000 0100
0x08 = 0000 0000 0000 0000 0000 0000 0000 1000
0x10 = 0000 0000 0000 0000 0000 0000 0001 0000
0x20 = 0000 0000 0000 0000 0000 0000 0010 0000

So if you | those, you get a number for which each bit represents a setting.


| has a close relative in &. While | results in 0 only if both bits are 0, & results in 1 only if both bits are 1. You can use that to check if a specific bit is set:
if (bits & IS_INDEXED == 0) // IS_INDEXED is not set
if (bits & STORE_TERMVECTOR == 0) // STORE_TERMVECTOR is not set
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are very helpful for me.
Thanks very very much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!