?
Solved

Bit Comparison

Posted on 2003-03-12
7
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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