 |

08-22-2017, 07:19 AM
|
Newcomer
|
|
Join Date: Sep 2010
Posts: 22
|
|
Please Help Me With Remember How To Do This - Senior Moment
I think I am having a senior moment and have forgotten how to do a VB trick I used to use all the time and cannot find any forum to help my memory.
I used to have value 0 to 15 and I thought I used "AND" as in 2 AND 2 it would return a 1 (or so I thought) because that bit is set. Same thing as 15 AND 2 = 1 because in 1111 the second bit is set. However I am not remembering it quite right because 2 AND 2 actually equals 2.
My program just tests bits 8,4,2 and 1 (four least significant bits) to see if they are set and then my conditions are based simply on a result of 1 or 0. Not 2 or anything higher.
Can someone please help me remember how I used to take a decimal number 0 - 15 and test it for a specific bit 8 (fourth bit),4(third bit),2(second bit) or 1(first bit) and just have it return a 1 or 0? I know I am forgetting something and I hope I explained it well what I am trying to do.
0 AND 0 = 0
0 AND 1 = 0
0 AND 2 = 0
0 AND 4 = 0
0 AND 8 = 0
1 AND 0 = 0
1 AND 1 = 1
1 AND 2 = 0
1 AND 4 = 0
1 AND 8 = 0
2 AND 0 = 0
2 AND 1 = 0
2 AND 2 = 1
2 AND 4 = 0
2 AND 8 = 0
3 AND 0 = 0
3 AND 1 = 1
3 AND 2 = 1
3 AND 4 = 0
3 AND 8 = 0
etc...
15 AND 0 = 0
15 AND 1 = 1
15 AND 2 = 1
15 AND 4 = 1
15 AND 8 = 1
Thank you.

|
|

08-22-2017, 07:28 AM
|
 |
Contributor
|
|
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 671
|
|
Dim I As Integer
For I = 0 To 15
If (I And 1) = 1 Then Debug.Print(CStr(I) & " First Bit is Set")
If (I And 2) = 2 Then Debug.Print(CStr(I) & " Second Bit is Set")
If (I And 4) = 4 Then Debug.Print(CStr(I) & " Third Bit is Set")
If (I And 8) = 8 Then Debug.Print(CStr(I) & " Fourth Bit is Set")
Next
The Rule Beeing
(Integer And Bit ) = Bit
|
__________________
As finishing touch, god created the Dutch!
utterly useless, but always trying to help
|

08-22-2017, 07:54 AM
|
Newcomer
|
|
Join Date: Sep 2010
Posts: 22
|
|
That's it. Thank you. I turned 52 and it all went... ;-)
|
|

08-22-2017, 07:59 AM
|
 |
Contributor
|
|
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 671
|
|
your very welcome 
|
__________________
As finishing touch, god created the Dutch!
utterly useless, but always trying to help
|
Tags
|
bit, set, return, bits, moment, senior, remember, huh, decimal, 1first, conditions, based, simply, result, fourth, bit, 4third, hope, test, bit, 2second, forgetting, specific, explained, time, forum, memory  |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|