 |

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,688
|
|
|
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,688
|
|
|
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-07-2005, 09:24 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,688
|
|
|
For instance:
2 in binary is:
00000000000000000000000000000010
NOT 2:
11111111111111111111111111111101
+1:
11111111111111111111111111111110 aka -2
|
__________________
Quis custodiet ipsos custodues.
|

09-07-2005, 09:43 AM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
Ok!! Thank You 
|
|

09-07-2005, 12:09 PM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
|
When I do the Not(binary value) function, I get -11 for 2. How do I get the same value as you mentionned,i.e. 11111111111111111111111111111101 ?? And also, how do I do binary addition in VB?
|
|

09-07-2005, 02:23 PM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,688
|
|
Quote:
|
Originally Posted by girish_mu
When I do the Not(binary value) function, I get -11 for 2. How do I get the same value as you mentionned,i.e. 11111111111111111111111111111101 ?? And also, how do I do binary addition in VB?
|
Then there is something wrong with your function. When you binary NOT a series of bits their values are flipped. This is, if they were one initially they are then zero, and vise versa.
I got my value by putting 2 in windows calculator (scientific mode, Dec(imal) base, Dword), then pressing the +/- key, and last Bin(ary) base. A propertly writting DecToBin function will do the same.
|
__________________
Quis custodiet ipsos custodues.
|

09-07-2005, 03:43 PM
|
|
Contributor
|
|
Join Date: Sep 2003
Location: Tampa, FL
Posts: 474
|
|
|
Off the wall question, but OnError, what would this be useful for in a realworld scenario?
I see how this could be a school assignement question and understand what you guys are talking about, but I don't know where it would be applied.
Just curious.
|
|

09-07-2005, 04:05 PM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,688
|
|
Understanding what binary Not does leads us to everyday VB code like this:
Code:
mnuFoo.Checked = Not mnuFoo.Checked ' Toggle
Knowing the algorithm for converting any base to/from decimal allows us to write a function to do so. Suppose your boss wanted you to convert to/from base 3. Since there is no VB function to do it, you'd have to write one.
|
__________________
Quis custodiet ipsos custodues.
|

09-08-2005, 03:55 AM
|
|
Freshman
|
|
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
|
|
Thank you for the algorithms!! I wanted the code for a school assignment. By the way, no one has developped the function; they have just used the Oct$() function. That's why I wanted to get the code. Anyway, I will try to make my own function and if it does not work, I will use the Oct$() function.
Thank you!! 
|
|

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,688
|
|
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 |
Linear 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
|
|
|
|
|
|