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,688
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,688
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-07-2005, 09:24 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

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

For instance:

2 in binary is:
00000000000000000000000000000010

NOT 2:
11111111111111111111111111111101

+1:
11111111111111111111111111111110 aka -2
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #8  
Old 09-07-2005, 09:43 AM
girish_mu girish_mu is offline
Freshman
 
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
Default

Ok!! Thank You
Reply With Quote
  #9  
Old 09-07-2005, 12:09 PM
girish_mu girish_mu is offline
Freshman
 
Join Date: Jan 2005
Location: Mauritius Island
Posts: 36
Default

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?
Reply With Quote
  #10  
Old 09-07-2005, 02:23 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

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

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.
Reply With Quote
  #11  
Old 09-07-2005, 03:43 PM
Zumwalt Zumwalt is offline
Contributor
 
Join Date: Sep 2003
Location: Tampa, FL
Posts: 474
Default

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.
Reply With Quote
  #12  
Old 09-07-2005, 04:05 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

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

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

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!!
Reply With Quote
  #14  
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
  #15  
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
  #16  
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
  #17  
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
  #18  
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,688
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
 
 
-->