• Kipper

    v0.12.1
  • Downloads

  • Docs

  • Playground

  • Changelog

  • Kipper

    v0.12.1
    • Downloads

    • Docs

    • Playground

    • Changelog

      • next /
      • Expressions /
      • Bitwise Expression
    Kipper Docs
    • next
    • latest
    • 0.11.0
    • 0.10.4
    • 0.9.2
    • Overview

    • Quickstart

    • Goals for Kipper

    • Supported platforms

    • Usage Examples

      • Overview

    • Variables

      • Overview

      • String Type

      • Number Type

      • Boolean Type

      • Void Type

      • Null Type

      • Undefined Type

      • Array Type

      • Overview

      • Tangled Expression

      • Arithmetic Expression

      • Assignment Expression

      • Conditional Expressions

      • Logical Expressions

      • Bitwise Expression

      • Relational Expressions

      • Convert Or Cast Expression

      • F-String Expression

      • Member Access Expression

      • Function Call Expression

      • Lambda Expression

      • Matches Expression

      • Overview

      • Expression Statement

      • If Statement

      • While Loop

      • Do-While Loop

      • For Loop

      • Compound Statement

      • Jump Statement

    • Functions

    • Interfaces

    • Classes

    • Comments

    • Built-in Functions

      • Overview

      • Compiler Setup

        • Overview

          • index

          • compiler

          • errors

          • logger

          • utils

          • config

          • index

          • index

        • Overview

        • new

        • run

        • compile

        • help

        • version

      • next /
      • Expressions /
      • Bitwise Expression
    • next /
    • Expressions /
    • Bitwise Expression

    Edit page

    Bitwise Expression

    Bitwise expressions are used to perform bitwise operations on integers. These operations are performed on the binary representation of the numbers and manipulate the individual bits of the number.

    Syntax

    Bitwise AND, OR and XOR

    EXP ( & | | | ^ ) EXP

    Bitwise NOT

    ~VALUE;

    Bitwise Shift

    EXP ( << | >> | >>>) EXP

    Examples

    Bitwise AND

    // Bitwise AND
    5 & 3; // -> 1

    Bitwise OR

    // Bitwise OR
    5 | 3; // -> 7

    Bitwise XOR

    // Bitwise XOR
    5 ^ 3; // -> 6

    Bitwise NOT

    // Bitwise NOT
    ~5; // -> -6

    Bitwise Shift

    // Bitwise Shift
    5 << 3; // -> 40
    5 >> 3; // -> 0
    5 >>> 3; // -> 0

    Chained Bitwise Expressions

    Bitwise expressions may be also chained together as long as you want. For example:

    // Chained Bitwise AND
    5 & 3 & 1; // -> 1
    
    // Chained Bitwise OR
    5 | 3 | 1; // -> 7
    
    // Chained Bitwise XOR
    5 ^ 3 ^ 1; // -> 7

    Precedence

    The precedence of bitwise expressions is as follows:

    1. Bitwise Shift
    2. Bitwise AND
    3. Bitwise XOR
    4. Bitwise OR

    Chained Bitwise Expressions

    Chained bitwise expressions are evaluated from left to right.

    5 & 3 | 1; // -> 1

    In this example, the bitwise AND operation is evaluated first, followed by the bitwise OR operation.

    You can also use parentheses to change the order of evaluation.

    5 & (3 | 1); // -> 1

    A more complicated example may look like this:

    // Declare variables
    var a: num = 15; // Binary: 1111
    var b: num = 10; // Binary: 1010
    var c: num = 3;  // Binary: 0011
    
    // Complex expression with all bitwise operations
    // Following the order of precedence:
    // 1. Bitwise Shift: a << 2 and then >> 3
    // 2. Bitwise AND: a & b
    // 3. Bitwise XOR: c ^ (result of shift operations)
    // 4. Bitwise OR: (result of AND operation) | (result of XOR operation)
    var result: num = a & b | c ^ a << 2 >> 3 >>> 4;
    
    // Output the result
    print(result as str);

    See Also

    • Arithmetic Expression
    • Kipper Operators and Expressions Precedence
  • Developed at:

    • Releases
    • GitHub
    • Security
    • Issue Tracker
    • License
  • Copyright © 2021-2025 Luna Klatzer, 2024-2025 Lorenz Holzbauer & Fabian Baitura.
    Kipper is licensed under the GPL-3.0-or-later license.