Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Binary to 2's complement


Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2005, 07:05 AM
girish_mu girish_mu is offline
Freshman
 
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
Default 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?
Reply With Quote
  #2  
Old 09-07-2005, 07:24 AM
BobThePenguin's Avatar
BobThePenguin BobThePenguin is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 397
Default

Reply With Quote
  #3  
Old 09-07-2005, 07:47 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #4  
Old 09-07-2005, 08:48 AM
girish_mu girish_mu is offline
Freshman
 
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
Default

huh....any piece of coding available?? By the way, I don't want to use the Oct$() function...
Reply With Quote
  #5  
Old 09-07-2005, 09:08 AM
BobThePenguin's Avatar
BobThePenguin BobThePenguin is offline
Junior Contributor
 
Join Date: Apr 2005
Posts: 397
Default

well im not going to give you any code but a 5 second google search should get you what you need...
Reply With Quote
  #6  
Old 09-07-2005, 09:21 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #7  
Old 09-09-2005, 11:12 AM
girish_mu girish_mu is offline
Freshman
 
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
Default 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 ???
Reply With Quote
  #8  
Old 09-09-2005, 11:27 AM
GavinO's Avatar
GavinO GavinO is offline
Coder of Fortune

Retired Leader
* Expert *
 
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
Default

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
Reply With Quote
  #9  
Old 09-09-2005, 11:32 AM
Val's Avatar
Val Val is offline
Senior Contributor
 
Join Date: May 2003
Location: Vancouver, WA
Posts: 1,268
Default

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
Reply With Quote
  #10  
Old 09-09-2005, 11:40 AM
girish_mu girish_mu is offline
Freshman
 
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
Default

Thank You (both of u)!!
Reply With Quote
  #11  
Old 09-09-2005, 11:44 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->