 |

09-07-2005, 07:05 AM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
Binary to 2's complement
|
I would like to get a picece of coding that converts a binary value to its 2's complement. And also, how to convert the 2's complement into octal?
|
|

09-07-2005, 07:24 AM
|
 |
Junior Contributor
|
|
Join Date: Apr 2005
Posts: 397
|
|
|

09-07-2005, 07:47 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
|
The neg instruction takes care of two's complement negation. This is simply the - operator in VB. The Oct$() function converts decimal numbers to octal base.
|
__________________
Quis custodiet ipsos custodues.
|

09-07-2005, 08:48 AM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
|
huh....any piece of coding available?? By the way, I don't want to use the Oct$() function...
|
|

09-07-2005, 09:08 AM
|
 |
Junior Contributor
|
|
Join Date: Apr 2005
Posts: 397
|
|
well im not going to give you any code but a 5 second google search should get you what you need... 
|
|

09-07-2005, 09:21 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
|
This sounds more like a school assignment now. If you'd like to perform your own two's compliment, then first take the Not of the number then add one.
Conversion to another base requires a simple loop, the modulus operator and integer division (\).
|
__________________
Quis custodiet ipsos custodues.
|

09-09-2005, 11:12 AM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
Not operator with binary
I have the following binary: 000000000010111
When I use
Code:
Not(000000000010111)
, it is automatically reduced to Not (10111) and the result obtained is -10112 .
How do I get 1111111111101000 ???
|
|

09-09-2005, 11:27 AM
|
 |
Coder of Fortune
Retired Leader * Expert *
|
|
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
|
|
|
VB is interpreting that number as an integer literal in base 10, if that is the actual code that you're running. VB doesn't have a facility for base 2 integer literals. There are various snippets about for interpreting strings in this way, but doing that at runtime is rather costly. Better would be to work out your base 2 literals in base 16, which VB does support with the &H prefix. Additionally, if you want a literal to be interpreted as a Long (32-bit) when it is small enough to be an Integer (16-bit), you need to suffix the literal with &. For example, 11111111 as a 32-bit integer:
&HFF&
|
__________________
-- The Gavster
Like to IRC? Try irc.randomirc.com
GavServer
|

09-09-2005, 11:32 AM
|
 |
Senior Contributor
|
|
Join Date: May 2003
Location: Vancouver, WA
Posts: 1,268
|
|
If I understood you correctly -
You are trying to invert a binary number by directly entering it into Visual Basic. As far as I know, Visual Basic does not understand binary numbers. If you want to do something of that sort, you use C++.
In other words, when you enter 000000000010111, Visual Basic interprets it as a decimal number 10111, ignoring all the zeroes up front. The inverse of 10111 is -10112, which is absolutely correct. The bottom line is - it is impossible to directly enter a binary number into Visual Basic. You either have to use another language, or a workaround.
One possibly workaround is to treat a binary number as a string, and add conversion functions to convert the binary number to and from bytes. All the operations, though, will work directly on a string. This will be horribly slow, but it will work.
For example, a Not function on a binary number expressed as a string:
Code:
Function SB_NOT(ByVal binum As String) As String
Dim biret As String
Dim i As Integer
Dim iLen As Integer
iLen = Len(binum)
For i = 1 To iLen
biret = biret & IIf(Mid$(binum, i, 1) = "1", "0", "1")
Next
SB_NOT = biret
End Function
|
|

09-09-2005, 11:40 AM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
Thank You (both of u)!! 
|
|

09-09-2005, 11:44 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
I suggest you operate on longs and then convert to binary or hex, much like Gavin mentioned. If you've read my tutorial linked in the second post of this thread, then you should understand the relationship between binary and hex.
Code:
Public Function NotNumber(Byval lIn As Long) As Long
NotNumber = Not(lIn)
End Function
From there make a string function to convert from a long to binary (string), or just use Hex$.
|
__________________
Quis custodiet ipsos custodues.
Last edited by OnErr0r; 09-09-2005 at 11:51 AM.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|