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

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 4
Reputation: praksk is an unknown quantity at this point 
Solved Threads: 0
praksk praksk is offline Offline
Newbie Poster

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

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

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

 
0
  #2
Jul 25th, 2008
Hi,

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

Regards
Veena
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 36
Reputation: Teropod is an unknown quantity at this point 
Solved Threads: 6
Teropod Teropod is offline Offline
Light Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
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

 
0
  #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 10:30 am.
-- Martín
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 4
Reputation: praksk is an unknown quantity at this point 
Solved Threads: 0
praksk praksk is offline Offline
Newbie Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
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

 
0
  #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 1:26 pm.
-- Martín
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 36
Reputation: Teropod is an unknown quantity at this point 
Solved Threads: 6
Teropod Teropod is offline Offline
Light Poster

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

 
0
  #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?

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Function BinToDec(strBin As String) As Long
  2. 'If return value is -1 please check strBin value!
  3.  
  4. Dim lonBinLenght As Long
  5. Dim lonDecMirror As Long
  6.  
  7. On Error GoTo Errorhandler
  8.  
  9. lonBinLenght = Len(strBin)
  10.  
  11. For i = 0 To lonBinLenght - 1
  12. If (Mid(strBin, lonBinLenght - i, 1)) > 1 Then GoTo Errorhandler
  13.  
  14. lonDecMirror = lonDecMirror + (Mid(strBin, lonBinLenght - i, 1) * 2 ^ i)
  15.  
  16. Next
  17.  
  18. BinToDec = lonDecMirror
  19.  
  20. Exit Function
  21. Errorhandler:
  22. strBin = -1
  23. End Function
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
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

 
0
  #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 Quick reply to this message  
Join Date: Jul 2008
Posts: 36
Reputation: Teropod is an unknown quantity at this point 
Solved Threads: 6
Teropod Teropod is offline Offline
Light Poster

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

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

Its also simple.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Function DecToBin(lonDec As Long, lonNumberOfBits As Long) As String
  2. Dim strBinMirror As String
  3. Dim intBit As Integer
  4. Dim lonDecMirror As Long
  5.  
  6. lonDecMirror = lonDec
  7.  
  8. Do While lonDecMirror > 0
  9. intBit = lonDecMirror Mod 2
  10. lonDecMirror = Fix(lonDecMirror / 2)
  11. strBinMirror = intBit & strBinMirror
  12. Loop
  13.  
  14. If Len(strBinMirror) < lonNumberOfBits Then
  15. For i = 1 To lonNumberOfBits - Len(strBinMirror)
  16. strBinMirror = "0" & strBinMirror
  17. Next
  18. End If
  19.  
  20. DecToBin = strBinMirror
  21. End Function

Hope these two functions will help!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 51
Reputation: dmf1978 is an unknown quantity at this point 
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

 
0
  #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 7:30 pm.
-- Martín
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC