Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Bit Comparison

Posted on 2003-03-12
7
Medium Priority
?
287 Views
Last Modified: 2010-04-07
Hi,

Wondering how i can do a bit comparison. From the port, with nothing connected the value is 01111000. I would like to test only the bits 4-7 i.e. 0XXXX000. What Command can i use that will return ONLY this value i.e. x = 1010 or 1111.

Also how can i single out the msb. 0XXXXXXX and have the value returned if its high or low.

thanks
0
Comment
Question by:mikesmith123
7 Comments
 
LVL 2

Expert Comment

by:navneet77
ID: 8123947
Here is a link for you

http://www.freevbcode.com/ShowCode.Asp?ID=2045

Use the shift functions and "AND" operator to get the bits.
0
 
LVL 11

Expert Comment

by:supunr
ID: 8124195
OK, how about this?

' FromBit > ToBit
Private Function Bits(Number As Long, FromBit As Integer, ToBit As Integer) As Long
    Dim i As Long
   
    Bits = 0
    For i = FromBit To ToBit Step -1
        Bits = Bits Or (Number And (2 ^ i))
    Next i
    Bits = Bits / (2 ^ ToBit)
End Function

just say,

Bits(13, 3, 2) ' bits 3 to 2 of 13 = 3

Good Luck!
0
 

Author Comment

by:mikesmith123
ID: 8124318
I am not sure i understand the code, or how to put it in my form. this go to both responses.

please help some more
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
navneet77 earned 300 total points
ID: 8124459
to get the four bits

n = rshift(portnumber,4) AND 15 '15 = 00001111
this will give a number (n) with only the four bits you wanted

and about
>Also how can i single out the msb. 0XXXXXXX and have the value returned if its high or low.
i did not understand what do you mean by it exactly and what is msb

0
 
LVL 11

Expert Comment

by:supunr
ID: 8124475
with my Bits function as shown above, you could do...

Private Sub YourFunction()
    DIm PortInputVal as long
    Dim X as long

    PortInputVal = GetValueFromThePortFunction()

    x = Bits(PortInputVal, 7, 4) ' Get Bits 7 to 4
    ' rest of your code...
End Sub

Hope this is useful.
0
 
LVL 1

Expert Comment

by:hj2k3
ID: 8124629
navneet77: msb is 'most significant bit', i.e leftmost in standard notation.

to single it out you could do a right shift 8, i.e
msb = rshift(PortInputVal, 8)

or the equivalent would be to AND it with 128 (10000000)
msb = PortInputVal AND 128
...which would probably be preferable (faster)
msb would then be high (1) or low (0)

you can get the four other specific bits by ANDing with 120 (01111000)
bits4to7 = PortInputVal AND 120
0
 
LVL 2

Expert Comment

by:navneet77
ID: 8124865
Thanks hj2k3 i get it now
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question