Mod vs And: What's And doing in this Code??

erock24

Well-known Member
Joined
Oct 26, 2006
Messages
1,163
I get the same answer using MOD as I do using AND with regards to the code below. Note the difference of using 256 vs 255. Could someone explain what the AND is doing, I don't get it and I can't find the answer online. Never seen it used like this before. Just curious. Thanks for your time:

Code:
r = xNUM And 255
x = xNUM Mod 256
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
xNUM and 255 reduces the value of xNUM to its lowest 8 bits which effectively is a MOD 256.
 
Last edited:
Upvote 0
Last edited:
Upvote 0
Run this code. Same answer is returned. I don't get it ??
Code:
Public Sub Test()
    Dim xNUM As Long
    xNUM = 1234567
    Debug.Print xNUM And 255
    Debug.Print xNUM Mod 256
End Sub
 
Upvote 0
It's just an example of an identity; num Mod 2^n == num And (2^n - 1) (for num >=0)

So num Mod 8 = num And 7&, num Mod 32768 = num And 32767&, ...

If you look at the numbers in binary, it's pretty clear why that's true.
 
Last edited:
Upvote 0
Consider the binary number yyyyyyyyxxx, where the x's and y's are randomly 1 or 0.

o yyyyyyyyxxx And 00000000111 (i.e., 7) returns xxx, and ...

o yyyyyyyy000 is clearly a multiple of 8 (in the same way that 12345000 is a clearly a multiple of 1000), so ...

o yyyyyyyyxxx And 7 is the same as yyyyyyyyxxx Mod 8
 
Upvote 0
The AND operator can also perform bitwise logical operation on integral values.

Bitwise Operations
Bitwise operations evaluate two integral values in binary (base 2) form. They compare the bits at corresponding positions and then assign values based on the comparison. The following example illustrates the And operator.

Dim x As Integer
x = 3 And 5


The preceding example sets the value of x to 1. This happens for the following reasons:
The values are treated as binary:
3 in binary form = 011
5 in binary form = 101
The And operator compares the binary representations, one binary position (bit) at a time. If both bits at a given position are 1, then a 1 is placed in that position in the result. If either bit is 0, then a 0 is placed in that position in the result. In the preceding example this works out as follows:
011 (3 in binary form)
101 (5 in binary form)
001 (The result, in binary form)

The result is treated as decimal. The value 001 is the binary representation of 1, so x = 1.

The bitwise Or operation is similar, except that a 1 is assigned to the result bit if either or both of the compared bits is 1. Xor assigns a 1 to the result bit if exactly one of the compared bits (not both) is 1. Not takes a single operand and inverts all the bits, including the sign bit, and assigns that value to the result. This means that for signed positive numbers, Not always returns a negative value, and for negative numbers, Not always returns a positive or zero value.

Logical/Bitwise Operators
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top