Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Tech Discussions (http://www.xtremevbtalk.com/tech-discussions/)
-   -   Please Help Me With Remember How To Do This - Senior Moment (http://www.xtremevbtalk.com/tech-discussions/328726-please-help-remember-senior-moment.html)

zunebuggy 08-22-2017 08:19 AM

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.
:huh:

Goggy 08-22-2017 08:28 AM

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

zunebuggy 08-22-2017 08:54 AM

That's it. Thank you. I turned 52 and it all went... ;-)

Goggy 08-22-2017 08:59 AM

your very welcome :)


All times are GMT -6. The time now is 10:08 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.