<!--
{
  "availability" : [
    "iOS: 14.0.0 - 18.0.0",
    "iPadOS: 14.0.0 - 18.0.0",
    "macCatalyst: 14.0.0 - 18.0.0",
    "macOS: 11.0.0 - 15.0.0",
    "tvOS: 14.0.0 - 18.0.0",
    "visionOS: 1.0.0 - 2.0.0",
    "watchOS: 7.0.0 - 11.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSArithmeticBinary",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@SA@BNNSArithmeticBinary"
  },
  "title" : "BNNSArithmeticBinary"
}
-->

# BNNSArithmeticBinary

A structure that contains the inputs and output of an arithmetic operation with two inputs.

```
struct BNNSArithmeticBinary
```

## Overview

Use a [`BNNSArithmeticUnary`](/documentation/Accelerate/BNNSArithmeticUnary) structure to pass the descriptions and types of the inputs and output of a binary arithmetic operation to [`BNNSLayerParametersArithmetic`](/documentation/Accelerate/BNNSLayerParametersArithmetic).

The following code shows how to calculate the element-wise sums of two vectors:

```swift
let inputOne: [Float] = [ 1,  2,  3,  4,  5,  6,  7,  8,  9,  10]
let inputTwo: [Float] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
let count = inputOne.count
var outputs = [Float](repeating: 0,
                      count: count)

let descriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                       layout: BNNSDataLayoutVector,
                                       size: (count, 0, 0, 0, 0, 0, 0, 0),
                                       stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                       data: nil,
                                       data_type: .float,
                                       table_data: nil,
                                       table_data_type: .float,
                                       data_scale: 1,
                                       data_bias: 0)

var fields = BNNSArithmeticBinary(in1: descriptor, in1_type: BNNSSample,
                                  in2: descriptor, in2_type: BNNSConstant,
                                  out: descriptor, out_type: BNNSSample)

let function = BNNSArithmeticAdd

withUnsafeMutableBytes(of: &fields) { fieldsPtr in
    
    var layerParameters = BNNSLayerParametersArithmetic(arithmetic_function: function,
                                                        arithmetic_function_fields: fieldsPtr.baseAddress!,
                                                        activation: .identity)
    
    guard let arithmeticLayer = BNNSFilterCreateLayerArithmetic(&layerParameters, nil) else {
        print("Binary BNNSFilterCreateLayerArithmetic returned nil")
        return
    }
    defer {
        BNNSFilterDestroy(arithmeticLayer)
    }
    
    inputOne.withUnsafeBytes { in1Ptr in
        inputTwo.withUnsafeBytes { in2Ptr in
            
            var input = [in1Ptr.baseAddress!, in2Ptr.baseAddress!]
            
            let error = BNNSArithmeticFilterApplyBatch(arithmeticLayer,
                                                       1,
                                                       2,
                                                       &input,
                                                       [inputOne.count, inputTwo.count],
                                                       &outputs,
                                                       outputs.count)
            
            print("BNNSArithmeticFilterApplyBatch: error", error)
        }
    }
}

// Prints "[11.0, 22.0, 33.0, 44.0, 55.0, 66.0, 77.0, 88.0, 99.0, 110.0]"
print("Binary Arithmetic: outputs", outputs)
```

## Topics

### Creating an Arithmetic Structure

[`init(in1:in1_type:in2:in2_type:out:out_type:)`](/documentation/Accelerate/BNNSArithmeticBinary/init(in1:in1_type:in2:in2_type:out:out_type:))

Returns a new arithmetic structure that takes two inputs from the specified parameters.

[`init()`](/documentation/Accelerate/BNNSArithmeticBinary/init())

Returns a new arithmetic structure that takes two inputs.

### Inspecting the Properties of an Arithmetic Structure

[`in1`](/documentation/Accelerate/BNNSArithmeticBinary/in1)

The descriptor of the first input.

[`in1_type`](/documentation/Accelerate/BNNSArithmeticBinary/in1_type)

The descriptor type of the first input.

[`in2`](/documentation/Accelerate/BNNSArithmeticBinary/in2)

The descriptor of the second input.

[`in2_type`](/documentation/Accelerate/BNNSArithmeticBinary/in2_type)

The descriptor type of the second input.

[`out`](/documentation/Accelerate/BNNSArithmeticBinary/out)

The descriptor of the output.

[`out_type`](/documentation/Accelerate/BNNSArithmeticBinary/out_type)

The descriptor type of the output.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)