473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Explain: Using Or between longs during Assignment

TheSmileyCoder
2,322 Expert Mod 2GB
I am working on a project, in which I want to make a backup of the backend before a baseline if performed. Last time I needed a backup from the IT department it took them more then a week to restore a file, and then they restored it without giving me the permission to read it, which took another week. Therefore I need to make a backup myself before baseline.

I first tried using FileCopy, but run into trouble because the file is being marked in use. I then found this by Dev Ashish:
http://access.mvps.org/access/api/api0026.htm
that I could use.

I've implemented it and it works fine. However I am having trouble understanding the logic behind one of the code lines, namely line 5 of the below:

Expand|Select|Wrap|Line Numbers
  1. Private Const FOF_RENAMEONCOLLISION As Long = &H8
  2. Private Const FOF_FILESONLY As Long = &H80
  3. Private Const FOF_SIMPLEPROGRESS As Long = &H100
  4.  
  5. lngFlags = FOF_SIMPLEPROGRESS Or _
  6.            FOF_FILESONLY Or _
  7.            FOF_RENAMEONCOLLISION
I simply dont understand how using OR between differnt longs can give any kind of sensible output (Though I am sure it makes sense somehow).

So could anyone please explain what line 5 does?
Dec 8 '11 #1

✓ answered by sierra7

I have played some more!

I confirm that by using arbitary numbers you seem to get odd results but Help say that when using numerical expressions 'Or' does a bit-wise comparison.

So it's converting everything into binary. As we have three values we will have three virtual rows. If any column position has a '1' then that column's result is '1' and only if all column values are '0' will the result be zero. Hence 8 (1000) 0r 128 (10000000) 0r 256 (110000000) equates to (110001000) or 392.

Using your example with "7 or 9" gives 7 (111) 0r 9 (1001) = 15 (1111)
The two 'ones' from the RH colum are interpreted as True + True = True (i.e. 1 + 1 = 1)
You learn something every day! Although I dont know when I will make use of this!

I suppose the RGB() function is doing something similar when adding elements of Red, Green & Blue to generate a colour number.

Thanks for the entertainment!
S7

8 1377
sierra7
446 Expert 256MB
Hi Smiley
It adds the values together!

It's a new one on me but if you put a break point and debug it (or in my case insert message boxes) you can show this.

S7
Dec 8 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
No thats not exactly the case. At least I dont believe so. Typing in the immediate pane:
Expand|Select|Wrap|Line Numbers
  1. ? 7 or 9=15
  2. ? 255 or 110=255
  3. ? 78 or 87=95
So its not addition as far as I can see.
Dec 8 '11 #3
sierra7
446 Expert 256MB
This is nuts isn't it?

In your first example the values where all HEX and the values of &H8, &H80 and &H100 evaluated to 8,128,256 and lngFlags to 392 which is the total.

I shall play some more!
S7
Dec 8 '11 #4
sierra7
446 Expert 256MB
I have played some more!

I confirm that by using arbitary numbers you seem to get odd results but Help say that when using numerical expressions 'Or' does a bit-wise comparison.

So it's converting everything into binary. As we have three values we will have three virtual rows. If any column position has a '1' then that column's result is '1' and only if all column values are '0' will the result be zero. Hence 8 (1000) 0r 128 (10000000) 0r 256 (110000000) equates to (110001000) or 392.

Using your example with "7 or 9" gives 7 (111) 0r 9 (1001) = 15 (1111)
The two 'ones' from the RH colum are interpreted as True + True = True (i.e. 1 + 1 = 1)
You learn something every day! Although I dont know when I will make use of this!

I suppose the RGB() function is doing something similar when adding elements of Red, Green & Blue to generate a colour number.

Thanks for the entertainment!
S7
Dec 8 '11 #5
NeoPa
32,556 Expert Mod 16PB
S7:
I suppose the RGB() function is doing something similar when adding elements of Red, Green & Blue to generate a colour number.
Absolutely.

In fact it's a bitwise OR. You've pretty well explained it already. It treats each bit separately and the result of each bit resolves to 1 or 0 depending on the two bits found in the original values at the same bit position. AND, NOT and XOR work in similar ways. This type of processing is very useful for managing multiple flags in a single value. For a good example of this see the Buttons parameter of the MsgBox() function.

Notice that each usable value is a power of two so that it directly represents a number which is a single 1 bit with all the rest 0s. In such simple scenarios adding is perfectly possible and works. In anything more complex though that would not work reliably when the OR is what is actually required (So use OR rather than + when dealing with such things for preference).

Another thing you may find somewhat surprising is that these bitwise operations are exactly those used with the same operators when using If or IIf(). Hence the value True is actually -1 (all bits = 1) rather than 1 (only bit 0 = 1). Also, any non-zero value is treated as True (or, at least, not False). Try it sometime :

Expand|Select|Wrap|Line Numbers
  1. If 7 And 24 Then Debug.Print "True"
  2. If 9 And 24 Then Debug.Print "True"
None of the 1 bits of the first one overlap, so nothing is printed for that one, but in the second one bit 3 (=8) is common to both therefore "True" is printed to the Immediate Pane.
Dec 9 '11 #6
NeoPa
32,556 Expert Mod 16PB
S7:
The two 'ones' from the RH colum are interpreted as True + True = True (i.e. 1 + 1 = 1)
Absolutely again. This is Boolean Arithmetic invented by George Boole. A fundamental of logic explanation and processing.
Dec 9 '11 #7
TheSmileyCoder
2,322 Expert Mod 2GB
First, thanks to both of you for taking the time to explain so well.

NeoPa
Another thing you may find somewhat surprising is that these bitwise operations are exactly those used with the same operators when using If or IIf(). Hence the value True is actually -1 (all bits = 1) rather than 1 (only bit 0 = 1). Also, any non-zero value is treated as True (or, at least, not False).
I do not understand the part about true being all bits=1.... Any chance of more explanation on that?


Ok, so especially your example using this with AND starts to make some sense to me, in terms of practical application.
I did this small code as test to myself, all the shown vb constants are part of the constants used for the msgbox function:
Expand|Select|Wrap|Line Numbers
  1. Dim lngCombined  As Long
  2.     lngCombined = vbOKCancel Or vbCritical Or vbMsgBoxHelpButton
  3.  
  4.  
  5.     If vbOKCancel And lngCombined Then Debug.Print "vbOKCancel"
  6.     If vbCritical And lngCombined Then Debug.Print "vbCritical"
  7.     If vbMsgBoxHelpButton And lngCombined Then Debug.Print "vbMsgBoxHelpButton"
When you use a msgbox you might combine some of the parameters to specify both what buttons to display as well as whether a Question mark/Exclamation mark should appear. With your example, I now see how the msgbox function (probably) decodes the values given, something I have wondered about for some time.
Dec 9 '11 #8
NeoPa
32,556 Expert Mod 16PB
Smiley:
With your example, I now see how the msgbox function (probably) decodes the values given, something I have wondered about for some time.
Absolutely yet again :-)

Smiley:
I do not understand the part about true being all bits=1.... Any chance of more explanation on that?
A pleasure.

For simplicity, we'll start by reducing our integer value to 4 bits - just to make the numbers smaller and easier to follow. We're dealing with signed values, so a 4-bit variable can handle from 0 (0h in Hex) to 15 (Fh) in unsigned or from -8 (8h) to 7 (7h) as signed. Let's just take a closer look at the signed interpretations of all the possible values, as this is how we typically refer to them :

Expand|Select|Wrap|Line Numbers
  1. Signed  Hex  Binary  Boolean
  2. -8       8h   1000    True    
  3. -7       9h   1001    True
  4. -6       Ah   1010    True
  5. -5       Bh   1011    True
  6. -4       Ch   1100    True
  7. -3       Dh   1101    True
  8. -2       Eh   1110    True
  9. -1       Fh   1111    True
  10.  0       0h   0000    False
  11.  1       1h   0001    True
  12.  2       2h   0010    True
  13.  3       3h   0011    True
  14.  4       4h   0100    True
  15.  5       5h   0101    True
  16.  6       6h   0110    True
  17.  7       7h   0111    True
While each value that isn't exactly zero triggers the True path in an If statement or IIf() function, only the value -1 is equal to the predefined value True (Hence these two entries only are highlighted).

If you haven't dealt with storage of negative numbers before then the reason they are stored as they are, to represent the numbers as they are (Instead of a single bit used as a pos/neg flag followed by the Absolute value of the number in the remaining bits for instance), is that binary arithmetic in computer processors work in such a way as to flow from negative to positive this way without any special handling. Consider these same binary values but treat them as unsigned. Let's now look at incrementing (A simple addition of one) -6, which is 10 (Ah) when viewed as unsigned. You would expect 10 to increment to 11 (Bh) yes? What do we notice as the signed verion of 11? -5. Let's now look at some values that overflow. To do this it may help to consider an extra bit (Binary-digIT) to the left of the four we have. This is usually handled as an Overflow flag in the processor, but it is sensible for us to understand it as an extra bit. We just ignore this bit once it's comleted the process.
Expand|Select|Wrap|Line Numbers
  1.  0 - 1 ==> 1 0000 - 0 0001 ==> 16 - 1 ==> 10h - 1h =  Fh ==> 15 ==> 0 1111 (or 1111) ==> -1
  2. -1 + 1 ==> 0 1111 + 0 0001 ==> 15 + 1 ==>  Fh + 1h = 10h ==> 16 ==> 1 0000 (or 0000) ==>  0
Does that all make sense? And does it answer your question adequately?
Dec 9 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Andy Lomax | last post by:
Is there any difference between: foo A = B; and foo A; A = B; Specifically, I have a case where B is a function call which returns a
2
by: Bill Moran | last post by:
Hello again, I'm developing a program based on PostgreSQL. It's consists of tables, constraints, _many_ stored procedures, 56M of test data, and a client app written in C. To help me with...
5
by: DW | last post by:
I have a query in Access 2003 that has the following criteria SELECT tblOrder.SessionDate, tblMenus.Item_Name, tblOrder.Type, Format$(tblorder!SessionDate,"Short Time") AS SessionTime,...
13
by: Jim Armstrong | last post by:
Hi all - This problem has been driving me crazy, and I'm hoping the answer is something stupid I am neglecting to see.... The procedure posted below is part of an Access/SQL database I have...
1
by: sang | last post by:
Sir My table name ss with the field cur_date data type varchar() show create table ss; +-------+-----------------------------------------------------------------------...
2
by: syaans | last post by:
What's the difference between intialization and assignment in c language?
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
1
by: Clint Stowers | last post by:
Did this once before long ago but forgot how. I remember that if you place a BMP file with the same name as your access application in the root directory that picture file will be displayed as...
3
by: Tony Lucas | last post by:
When using the WHERECondition "Between And". The dates range from the 1st to 9th do not filter in to the report but if I set the date from the 31st to 10th then they do. Do you know why this...
5
by: yappy77 | last post by:
I have a query with an IIF statement that returns #ERROR. The results I want are if the requalify date is between 1/1/"year does not matter" and 3/31/"year does not matter" then First Quarter. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.