Wheel IDE

Online demo »

Bit

Bit example

This example demonstrates how to read the bits from a decimal number.
This example gets a random number greater or equal then 0 and less than 256 and converts the number using the bitAnd function.

Code

#project "Bit demo"

#include "lib/modules/standard.whl"
#include "lib/modules/math.whl"
#include "lib/modules/bit.whl"

proc main()
    number n = random(0, 255)

    clearConsole()

    ; Print the decimal number in the console:
    printS("Decimal:")
    printN(n)

    string s   = ""
    number bit = 1
    number b
    for b = 1 to 8
        number bAnd = bitAnd(n, bit)
        if bAnd == bit
            s = s + "1"
        else
            s = s + "0"
        end
        bit *= 2
    end

    ; Print the binary string in the console:
    printS("Binary:")
    printS(s)
end
See also: Modules