X

NV5 Geospatial Blog

Each month, NV5 Geospatial posts new blog content across a variety of categories. Browse our latest posts below to learn about important geospatial information or use the search bar to find a specific topic or author. Stay informed of the latest blog posts, events, and technologies by joining our email list!



Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

6/3/2025

Rethinking the Reliability of Type 1a Supernovae   How do astronomers measure the universe? It all starts with distance. From gauging the size of a galaxy to calculating how fast the universe is expanding, measuring cosmic distances is essential to understanding everything in the sky. For nearby stars, astronomers use... Read More >

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

5/26/2025

Whether you’re new to remote sensing or a seasoned expert, there is no doubt that large language models (LLMs) like OpenAI’s ChatGPT or Google’s Gemini can be incredibly useful in many aspects of research. From exploring the electromagnetic spectrum to creating object detection models using the latest deep learning... Read More >

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

4/28/2025

When every second counts, the ability to process geospatial data rapidly and accurately isn’t just helpful, it’s critical. Geospatial Intelligence (GEOINT) has always played a pivotal role in defense, security, and disaster response. But in high-tempo operations, traditional workflows are no longer fast enough. Analysts are... Read More >

Thermal Infrared Echoes: Illuminating the Last Gasp of a Dying Star

Thermal Infrared Echoes: Illuminating the Last Gasp of a Dying Star

4/24/2025

This blog was written by Eli Dwek, Emeritus, NASA Goddard Space Flight Center, Greenbelt, MD and Research Fellow, Center for Astrophysics, Harvard & Smithsonian, Cambridge, MA. It is the fifth blog in a series showcasing our IDL® Fellows program which supports passionate retired IDL users who may need support to continue their work... Read More >

A New Era of Hyperspectral Imaging with ENVI® and Wyvern’s Open Data Program

A New Era of Hyperspectral Imaging with ENVI® and Wyvern’s Open Data Program

2/25/2025

This blog was written in collaboration with Adam O’Connor from Wyvern.   As hyperspectral imaging (HSI) continues to grow in importance, access to high-quality satellite data is key to unlocking new insights in environmental monitoring, agriculture, forestry, mining, security, energy infrastructure management, and more.... Read More >

1345678910Last
44499 Rate this article:
3.4

Logical vs. Bitwise Operators

Anonym

There are two different types of comparison operators in IDL: bitwise operators and logical operators.

A bitwise operator evaluates each bit of two expressions based on the logic that is defined by the operator. These include the AND, OR, NOT, and XOR operators. Each bit of the input expressions
will be compared independently of other bits. IDL will always evaluate both expressions. 

A logical operator is very similar to a bitwise operator in that it evaluates two conditions. IDL's logical operators are && (logical and), || (logical or), and ~ (logical not). There are two significant differences between a logical and bitwise operator. First, a logical operator will always return 1 (for true) or 0 (for false). Additionally, a logical operator will perform "short circuit" logic, meaning that if the outcome is known after only checking the first condition, the second condition is ignored. For example, when using the logical and, we know that both conditions must be true in order for the result to be true; if the first of the two evaluates to false, then the result will be false regardless of the second condition, and therefore the second condition will not be evaluated.

In addition to simply being a bit more efficient, the logical and is extremely useful when you first need to check for the existence of a value before checking a condition of it. For instance, I have a variable called var, and I want to write an IF statement that is executed if the variable is greater than 5. However, in my code, I don't know for sure that var exists, so first I must check N_ELEMENTS on it. With the logical and, this can be done on one line.

 

IF N_ELEMENTS(var) GT 0 && var GT 5 THEN BEGIN
   ; ...
ENDIF

 

If var doesn't exist in the code above, then IDL isn't going to check to see if var is greater than 5. If the bitwise AND operator was used, IDL would have thrown an undefined variable error when checking the second half of the if statement. To use AND, this logic would need to be on two different lines.

 

IF N_ELEMENTS(var) GT 0 THEN BEGIN
  IF var GT 5 THEN BEGIN
    ; ...
  ENDIF
ENDIF

 

So when would you want to use a bitwise operator? 

Well first of all, logical operators only work when the result from each expression is a scalar. For example, if I want to use a WHERE statement to check two different conditions on an array, then the bitwise operator needs to be used.

 

arr = INDGEN(6)
result = WHERE(arr GT 1 AND arr LT 4)

 

Substituting AND with && in the above expression would result in an error.

In less common cases, the conditional expressions might be the result of two different functions, and you want both functions to always be performed and not skipped (perhaps the functions do something to the variable as it's passed-by-reference, or maybe they change internal variables based on the input), then you could put them together with a bitwise operator.

 

IF my_function1(var) AND my_function2(var) THEN BEGIN
  ; ...
ENDIF

 

Note that if you wanted one to always run but didn't care about the second, you could use a logical operator and put the one you always want performed first.

You can even do tricks with bitwise logic. Because bitwise logic operates on all bits of each value and does so independently of all other bits, you get interesting results. For example:

 

PRINT, 3 AND 6

 

IDL will print 2. This is because 3 in binary is 011 and 6 in binary is 110. The result is... (first bit): 0 AND 1 = 0, (second bit): 1 AND 1 = 1, (third bit): 1 AND 0 = 0...  010, which is 2. This can sometimes be handy when processing image data to modify certain colors. A very useful example is that the logical NOT will convert a photograph to a negative.

As a final note, keep in mind that when multiple operations are performed in the same line of code, IDL will follow an operator precedence, similar to order of operations in mathematics.

 

Please login or register to post comments.