943,724 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jul 25th, 2008
0

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

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
praksk is offline Offline
4 posts
since Jul 2008
Jul 25th, 2008
0

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

Hi,

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

Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Jul 25th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 6
Light Poster
Teropod is offline Offline
36 posts
since Jul 2008
Jul 25th, 2008
0

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

Hi,
Try this

VB Syntax (Toggle Plain Text)
  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.
Reputation Points: 18
Solved Threads: 7
Junior Poster in Training
dmf1978 is offline Offline
51 posts
since Aug 2006
Jul 25th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
praksk is offline Offline
4 posts
since Jul 2008
Jul 25th, 2008
0

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

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)
VB Syntax (Toggle Plain Text)
  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.
Reputation Points: 18
Solved Threads: 7
Junior Poster in Training
dmf1978 is offline Offline
51 posts
since Aug 2006
Jul 25th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 6
Light Poster
Teropod is offline Offline
36 posts
since Jul 2008
Jul 25th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Teropod ...
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
Reputation Points: 18
Solved Threads: 7
Junior Poster in Training
dmf1978 is offline Offline
51 posts
since Aug 2006
Jul 25th, 2008
0

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

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!
Reputation Points: 10
Solved Threads: 6
Light Poster
Teropod is offline Offline
36 posts
since Jul 2008
Jul 25th, 2008
0

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

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.
Reputation Points: 18
Solved Threads: 7
Junior Poster in Training
dmf1978 is offline Offline
51 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Upload picture from file
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Need your opinion:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC