# Binary Arithmetic Classes?

Does anyone have any binary arithmetic classes they would be willing to share? I’m in the process of writing a class for calculating network subnetting and as IP address and subnet masks are really binary numbers, doing the operations in binary would be much simpler than what I am doing right now. I’ve got it all working but I figured if someone already has some classes I could use, it would make my life simpler.

Sure wish Xojo supported things like actual types for binary and hex numbers as opposed to having the use the literal characters which don’t even really work when you convert a 10 based number to binary using the bin function because then you end up with a string. If Bin gave me a binary number, that would be better.

Oh well…

Thanks.

A number is a number, so I’m not sure I follow.

To add binary “101” (5) and binary “11” (3) you could do this:

Dim b1 As String = “101”
Dim b2 As String = “11”

Dim n1 As Integer = Val("&b" + b1)
Dim n2 As Integer = Val("&b" + b2)

Dim sum As Integer = n1 + n2

Dim binaryValue As String = Bin(sum) // result is “1000” (8)

Paul beat me by 3 seconds
Note the same method works for Hex ("&h") and Octal ("&O")

and heck you can even mix the base types in the same equation if needed

I do have a BitBlock class that treats it’s data as bits instead of bytes, if that would help.

I would be interested in seeing/knowing what you have inside the BitBlock class.

[quote=124366:@Jon Ogden]Does anyone have any binary arithmetic classes they would be willing to share? I’m in the process of writing a class for calculating network subnetting and as IP address and subnet masks are really binary numbers, doing the operations in binary would be much simpler than what I am doing right now. I’ve got it all working but I figured if someone already has some classes I could use, it would make my life simpler.

Sure wish Xojo supported things like actual types for binary and hex numbers as opposed to having the use the literal characters which don’t even really work when you convert a 10 based number to binary using the bin function because then you end up with a string. If Bin gave me a binary number, that would be better.

Oh well…

Thanks.[/quote]
Jon I wrote subnet calculator class that convers decimal to 32bit decimal words and does the math the. Converts back to decimal. I found that this was the most scalable and efficient way to perform the calculations.

[quote=124369:@Paul Lefebvre]A number is a number, so I’m not sure I follow.

To add binary “101” (5) and binary “11” (3) you could do this:

Dim b1 As String = “101”
Dim b2 As String = “11”

Dim n1 As Integer = Val("&b" + b1)
Dim n2 As Integer = Val("&b" + b2)

Dim sum As Integer = n1 + n2

Dim binaryValue As String = Bin(sum) // result is “1000” (8)[/quote]

I know and that’s what I am doing. However, why then do we have Integer data types for that matter? Let’s just make everything 64 bit real numbers since you can do everything with just that.

It’s a pain in the but to do all the conversions back and forth and back and forth. And a binary number is NOT a string.

Would you care to share your code?

Look at it this way… ALL Integer number datatypes are a direct BINARY represation of the value…
So what sense/value would it be to create another datatype… when classes/methods/procedures can alter the input/processing and presentation layers so easily? and the same applies to HEX and OCT… why have special datatype when they are true INTEGERS?

I am at the moment writing a Programmers Calculator (yeah another one)… but in SWIFT, and there I am using the exact same methods as mentioned above.

plug :

https://forum.xojo.com/11010-announcing-intelligent-subnet-calculator-from-intelligent-visib

An IPV4 address (like 172.16.254.1) is just 4 bytes (32 bit)
Bitwise ands plus a handful of shifts is all you need

172 = &hAC = &b10101100
16 = &h10 = &b00010000
254 = &hFE = &b11111110
1 = &h01 = &b00000001

These are all exactly equivalent ways of visualizing the same value

IPV6 is 128 bit but you can still divide it into bytes & nibbles using bitwise functions & manipulate it

You seem a little confused about how numbers are actually stored/manipulated in your cpu. There are bitwise functions built into Xojo. What more do you need? You’re probably doing more conversions than you need to do. Mike went through this recently. Perhaps you could review his threads related to IP address manipulations.

Yes I have no problem sharing it with you, but I am at a pool party for my kids back to school thing

Ill post the link to the github project a bit later if that is ok.

My Subnet Calculator (www.intelligentsubnetcalculator.com) MAS app uses these classes.

Thanks
Mike

I sure did and Thanks to Tim and Kem I was able to finish it nicely. I will post as soon as I can this evening.

Jon,

I originally wrote these subnet calculator classes for an enterprise app I am working on, however it morphed into a user app that I sell on the MAS. It was easier for my to just clone that source code and strip it down completely until I just had the calculator shell with the Subnet classes. I tried my best to declutter it, but I have quite a bit of “other” UI user code in this project.

I have some custom events that I never actually did use which are available for you to use easily also.

HTH and please let me know if you have any questions.

https://github.com/IntelligentVisibility/SubnetCalculator

This project also holds all of my IPv4 and subnet mask user validation code (if you need that).
HTH,
Mike

Please don’t assume you know things about me.

I know numbers are really binary items in the CPU which is why I find it so amusing you cannot actually work in binary in Xojo yet no one seems bothered by it!

Yes, there are bitwise functions but they are for logical operations on bits not arithmetic.

A bitwise AND of 11001100 and 10001011 and the sum of those two numbers is completely different same thing with bitwise OR.

Bitwise AND = 10001000
Bitwise OR = 11111100
Sum = 101010111

[quote=124502:@Jon Ogden]Please don’t assume you know things about me.

I know numbers are really binary items in the CPU which is why I find it so amusing you cannot actually work in binary in Xojo yet no one seems bothered by it!

Yes, there are bitwise functions but they are for logical operations on bits not arithmetic.

A bitwise AND of 11001100 and 10001011 and the sum of those two numbers is completely different same thing with bitwise OR.

Bitwise AND = 10001000
Bitwise OR = 11111100
Sum = 101010111[/quote]

11001100 or 10001011 => 11001111 (looks like you transposed it)
I’m not bothered by it because working in hex decimal octal or binary is easy enough to switch between
Bitwise is for bitwise manipulation not JUST logical and / or / not etc

dim i as integer = &b11001100 // 204
dim j as integer = &b10001011 // 139
dim i1 as integer = i and j
dim i2 as integer = i or j
dim i3 as integer = i + j

Binary Arithmetic consists primarily of two types of operations…
Bitwise : AND, OR, NAND, NOR, XOR, SHL, SHR, ROL, ROR etc.
Math : Add, Subtract, Multiply, Divide (with the last two usually resulting in a truncated integer value)

The MATH can be done using the inbuilt operators

&b11001100 + &b10001011 = &b101010111 (same as 204+139 = 343)

while the bitwise can be done a few different ways
&b11001100 AND &b10001011 = 10001000 (same as 204 AND 139 = 136)
&b11001100 OR &b10001011 = 11001111 (same as 204 OR 139 = 207)

The BITWISE operators can do this and much more… and the more esoteric functions can be derived by combining the existing functions

And if you want to be sticky… EVERYTHING in a computer is done in BINARY math… INTEGER and DOUBLE datatypes are simply a presentation layer to help us poor humans understand…