User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 427,222 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,280 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 780 | Replies: 12 | Solved
Reply
Join Date: Jul 2008
Posts: 4
Reputation: praksk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
praksk praksk is offline Offline
Newbie Poster

Need to separate 16 bit binary to 4bit and 12 bit binary values

  #1  
Jul 25th, 2008
I am relatively new to visual basic. I need to take two 8 bit byte values, join them to makea 16 bit binary value and then separate the 4 msb as a 4bit binary vale and the 12 lsb as a 12 bit binary value.

Thanks in advance for helping me out.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Posts: 712
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #2  
Jul 25th, 2008
Hi,

Post Some Sample Data here..
For What Input , What is the Output you want...?

Regards
Veena
Reply With Quote  
Join Date: Jul 2008
Posts: 31
Reputation: Teropod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
Teropod Teropod is offline Offline
Light Poster

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #3  
Jul 25th, 2008
Do you have

high byte__________low byte
l1l1l1l1l1l1l1l1l____l1l1l1l1l1l1l1l1l

Or you have two bytes like
high byte=255
low byte=255?

Those are example numbers.
Reply With Quote  
Join Date: Aug 2006
Location: Argentina
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #4  
Jul 25th, 2008
Hi,
Try this

  1. Dim byteA As Long
  2. Dim byteB As Long
  3. Dim sumBytes As Long
  4. Dim fourBits As Long
  5. Dim twelveBits As Long
  6.  
  7. byteA = 32 ' 00100000b = 32d
  8. byteB = 64 ' 01000000b = 64d
  9.  
  10. ' Adding the two bytes to get a Word
  11. ' sumBytes = 0010000001000000b = 2048b
  12. sumBytes = byteA * 255 + byteB
  13.  
  14. ' Take the four higher bits
  15. ' using this mask 1111000000000000b = 61440d
  16. fourBits = sumBytes And 61440
  17.  
  18. ' Take the next twelve bits
  19. ' using this mask 0000111111111111b = 4095d
  20. twelveBits = sumBytes And 4095
  21.  
  22. MsgBox fourBits
  23. MsgBox twelveBits

Just change the values of byteA and byteB.

Hope it helps
Last edited by dmf1978 : Jul 25th, 2008 at 9:30 am.
-- Martín
Reply With Quote  
Join Date: Jul 2008
Posts: 4
Reputation: praksk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
praksk praksk is offline Offline
Newbie Poster

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #5  
Jul 25th, 2008
Hi Veena,Terapod, dmf1978

Thanks for your replies.

I see light in dmf1978's code. Using the code I am able to get the 12 bit integer as I need it, however, The 4 bits when separated are to be a nibble with values from 0 to 15.


Dim byteA As Byte
Dim byteB As Byte

byteA = 65 '01000001b = 65d
byteB = 32 '00100000b = 32d


( On joining the two bytes we get 0100000100100000 , and on masking the 4 bits (MSB) we get 000100100000 = 288d . Now I need to get the 4 bit nibble 0100 = 4d )


Best Regards,
praksk
Reply With Quote  
Join Date: Aug 2006
Location: Argentina
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #6  
Jul 25th, 2008
Hi,
Well, I guess you have to shift right the variable fourBits 12 positions.
Try this (I am not really sure it will work, because I have not VB here)
  1. Dim byteA As Long
  2. Dim byteB As Long
  3. Dim sumBytes As Long
  4. Dim fourBits As Long
  5. Dim twelveBits As Long
  6.  
  7. byteA = 32 ' 00100000b = 32d
  8. byteB = 64 ' 01000000b = 64d
  9.  
  10. ' Adding the two bytes to get a Word
  11. ' sumBytes = 0010000001000000b = 2048b
  12. sumBytes = byteA * 255 + byteB
  13.  
  14. ' Take the four higher bits
  15. ' using this mask 1111000000000000b = 61440d
  16. fourBits = sumBytes And 61440
  17.  
  18. ' Shift right twelve positions
  19. fourBits = fourBits / 2 ^ 12
  20.  
  21. ' Take the next twelve bits
  22. ' using this mask 0000111111111111b = 4095d
  23. twelveBits = sumBytes And 4095
  24.  
  25. MsgBox fourBits
  26. MsgBox twelveBits
Last edited by dmf1978 : Jul 25th, 2008 at 12:26 pm.
-- Martín
Reply With Quote  
Join Date: Jul 2008
Posts: 31
Reputation: Teropod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
Teropod Teropod is offline Offline
Light Poster

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #7  
Jul 25th, 2008
Thats all nice but when i make code in vb i usually try to make it more universal so if i need it i can easy use it on other application and or if i need change output value i can do it much easier by changing input values

For example make conversion from numbering systems vb don't have binary numeric format as much as i know.

I just hope that you wont have too many combination for those binary numbers.
Just make simple function that will convert string witch have "binary value" and convert it to long value. that should be max 10 lines of code for any binary value you put in there.

Just copy and paste these function in your module.
Use it and you wont have to worry how much is 1100101 in dec?

Function BinToDec(strBin As String) As Long
'If return value is -1 please check strBin value!

Dim lonBinLenght As Long
Dim lonDecMirror As Long
    
    On Error GoTo Errorhandler
    
    lonBinLenght = Len(strBin)
    
    For i = 0 To lonBinLenght - 1
        If (Mid(strBin, lonBinLenght - i, 1)) > 1 Then GoTo Errorhandler
        
        lonDecMirror = lonDecMirror + (Mid(strBin, lonBinLenght - i, 1) * 2 ^ i)
    
    Next
    
    BinToDec = lonDecMirror

Exit Function
Errorhandler:
    strBin = -1
End Function
Reply With Quote  
Join Date: Aug 2006
Location: Argentina
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #8  
Jul 25th, 2008
Originally Posted by Teropod View Post
Just make simple function that will convert string witch have "binary value" and convert it to long value. that should be max 10 lines of code for any binary value you put in there.

And it is nice too, but as he say, he don't have string binary values. All that I know is the existence of two bytes. I've coded taking account of these.
As you said... Have fun
-- Martín
Reply With Quote  
Join Date: Jul 2008
Posts: 31
Reputation: Teropod is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
Teropod Teropod is offline Offline
Light Poster

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #9  
Jul 25th, 2008
Then he can just use these function.

Its also simple.

Function DecToBin(lonDec As Long, lonNumberOfBits As Long) As String
Dim strBinMirror As String
Dim intBit As Integer
Dim lonDecMirror As Long
    
    lonDecMirror = lonDec
    
    Do While lonDecMirror > 0
        intBit = lonDecMirror Mod 2
        lonDecMirror = Fix(lonDecMirror / 2)
        strBinMirror = intBit & strBinMirror
    Loop

    If Len(strBinMirror) < lonNumberOfBits Then
        For i = 1 To lonNumberOfBits - Len(strBinMirror)
            strBinMirror = "0" & strBinMirror
        Next
    End If
            
    DecToBin = strBinMirror
End Function

Hope these two functions will help!
Reply With Quote  
Join Date: Aug 2006
Location: Argentina
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 7
dmf1978's Avatar
dmf1978 dmf1978 is offline Offline
Junior Poster in Training

Re: Need to separate 16 bit binary to 4bit and 12 bit binary values

  #10  
Jul 25th, 2008
HI,
I see your point. But what he need is to separate some bits from a word. To do this, the easy way is to use masks. The mask thing is not an invention of mine.

Take a look here: http://en.wikipedia.org/wiki/Mask_(computing)
It is the more straightforward way to do that. And it uses less code too.

Sure, I am with you that he must code this inside a function, passing the original word, start_bit, end_bit, etc.

Regards
Last edited by dmf1978 : Jul 25th, 2008 at 6:30 pm.
-- Martín
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 11:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC