YouTip LogoYouTip

Zig Operators

Operators and expressions are basic components used to perform various operations in programming languages. In Zig, operators can be divided into several categories, including arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators. The following is a detailed description and examples of each type of operator. ## Arithmetic Operators | Operator | Description | | --- | --- | | + | Addition | | - | Subtraction | | * | Multiplication | | / | Division | | % | Remainder (Modulo) | ## Example ```zig const std = @import("std"); pub fn main()void{ const a: i32 =5; const b: i32 =3; const add: i32 = a + b; const subtract: i32 = a - b; const multiply: i32 = a * b; const divide: i32 = a / b; const remainder: i32 = a % b; std.debug.print("a + b = {}n", .{add}); std.debug.print("a - b = {}n", .{subtract}); std.debug.print("a * b = {}n", .{multiply}); std.debug.print("a / b = {}n", .{divide}); std.debug.print("a % b = {}n", .{remainder}); } The compilation output is: a + b = 8 a - b = 2 a * b = 15 a / b = 1 a % b = 2 ## Relational Operators | Operator | Description | | --- | --- | | == | Equal to | | != | Not equal to | | > | Greater than | | = | Greater than or equal to | | b; const less:bool= a = b; const less_equal:bool= a b: {}n", .{greater}); std.debug.print("a = b: {}n", .{greater_equal}); std.debug.print("a b: true a = b: true a <= b: false ## Logical Operators | Operator | Description | | --- | --- | | and | Logical AND | | or | Logical OR | | ! | Logical NOT | ## Example ```zig const std = @import("std"); pub fn main()void{ const a:bool=true; const b:bool=false; const and_result:bool= a and b; const or_result:bool= a or b; const not_result:bool=!a; std.debug.print("a and b: {}n", .{and_result});// false std.debug.print("a or b: {}n", .{or_result});// true std.debug.print("!a: {}n", .{not_result});// false } The compilation output is: a and b: false a or b: true!a: false ## Bitwise Operators | Operator | Description | | --- | --- | | & | Bitwise AND | | | | Bitwise OR | | ^ | Bitwise XOR | | ~ | Bitwise NOT | | <> | Right shift | ## Example ```zig const std = @import("std"); pub fn main()void{ const a: i32 =5;// 0101 const b: i32 =3;// 0011 const bit_and: i32 = a & b;// 0001 const bit_or: i32 = a | b;// 0111 const bit_xor: i32 = a ^ b;// 0110 const bit_not: i32 = ~a;// 11111111111111111111111111111010 const left_shift: i32 = a <>1;// 0010 std.debug.print("a & b: {}n", .{bit_and}); std.debug.print("a | b: {}n", .{bit_or}); std.debug.print("a ^ b: {}n", .{bit_xor}); std.debug.print("~a: {}n", .{bit_not}); std.debug.print("a <> 1: {}n", .{right_shift}); } The compilation output is: a & b: 1 a | b: 7 a ^ b: 6~a: -6 a <> 1: 2 ## Assignment Operators | Operator | Description | | --- | --- | | = | Assignment | | += | Addition assignment | | -= | Subtraction assignment | | *= | Multiplication assignment | | /= | Division assignment | | %= | Remainder assignment | | &= | Bitwise AND assignment | | |= | Bitwise OR assignment | | ^= | Bitwise XOR assignment | | <>= | Right shift assignment | ## Example ```zig const std = @import("std"); pub fn main()void{ var a: i32 =5; const b: i32 =3; a += b;// equivalent to a = a + b; std.debug.print("a += b: {}n", .{a}); a -= b;// equivalent to a = a - b; std.debug.print("a -= b: {}n", .{a}); a *= b;// equivalent to a = a * b; std.debug.print("a *= b: {}n", .{a}); a = @divTrunc(a, b);// equivalent to a = a / b; std.debug.print("a /= b: {}n", .{a}); a = @mod(a, b);// equivalent to a = a % b; std.debug.print("a %= b: {}n", .{a}); a &= b;// equivalent to a = a & b; std.debug.print("a &= b: {}n", .{a}); a |= b;// equivalent to a = a | b; std.debug.print("a |= b: {}n", .{a}); a ^= b;// equivalent to a = a ^ b;
← Css Sel HasZig Loop β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.