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.
[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.
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.
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.
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.
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…