pxt-calliope/olddocs/js/bits.md

68 lines
2.1 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Bits Library
2016-04-02 01:22:47 +02:00
Functions in the Bits library.
2016-03-26 00:47:20 +01:00
### @parent td/language
The binary numeral system represents numeric values using values 0 and 1. This is how almost all modern computers store data. Each 0 or 1 digit is called a binary digit, or bit for short.
The Bits library includes functions for bit-level manipulation of integers. In the [Touch Develop editor](/javascript/editor), click `bits` to see the following bit functions:
2016-03-26 00:47:20 +01:00
## Bitwise and, or, and xor functions
#### Syntax
2017-03-16 15:57:41 +01:00
bits `->` *and/or/xor* uint32 (x : [Number](/types/number), y : [Number](/types/number)) *returns* [Number](/types/number)
2016-03-26 00:47:20 +01:00
#### Parameters
2017-03-16 15:57:41 +01:00
* x - an unsigned 32 bit integer [Number](/types/number)
* y - another unsigned 32 bit integer [Number](/types/number)
2016-03-26 00:47:20 +01:00
### and uint32
performs bitwise AND; returns `1` at position i if both bits *x[i]* and *y[i]* are `1`, otherwise returns `0`.
### or uint32
performs bitwise OR; returns `1` at position *i* if either bit *x[i]* or *y[i]* is `1`, otherwise returns `0`.
### xor uint32
performs bitwise exclusive XOR; returns `1` at position *i* if *x[i]=1 and y[i]=0* or *x[i] = 0 and y[i] =1*; returns `0` otherwise
## Rotate left and rotate right
Rotate bits to the left or the right, by the specified number of positions.
#### Syntax
2017-03-16 15:57:41 +01:00
bits `->` rotate left unint32 (x : [Number](/types/number), bits : [Number](/types/number)) *returns* [Number](/types/number)
2016-03-26 00:47:20 +01:00
2017-03-16 15:57:41 +01:00
bits `->` rotate right unint32 (x : [Number](/types/number), bits : [Number](/types/number)) *returns* [Number](/types/number)
2016-03-26 00:47:20 +01:00
#### Parameters
2017-03-16 15:57:41 +01:00
* x - [Number](/types/number);
* bits - [Number](/types/number);
2016-03-26 00:47:20 +01:00
## Shift left and shift right
Shift bits to the left or the right, by the specified number of positions.
#### Syntax
2017-03-16 15:57:41 +01:00
bits `->` shift left unint32 (x : [Number](/types/number), bits : [Number](/types/number)) *returns* [Number](/types/number)
2016-03-26 00:47:20 +01:00
2017-03-16 15:57:41 +01:00
bits `->` shift right unint32 (x : [Number](/types/number), bits : [Number](/types/number)) *returns* [Number](/types/number)
2016-03-26 00:47:20 +01:00
#### Parameters
2017-03-16 15:57:41 +01:00
* x - [Number](/types/number);
* bits - [Number](/types/number);
2016-03-26 00:47:20 +01:00
### See also
2017-03-16 15:57:41 +01:00
[statements and operators](/javascript/statements), [math functions](/javascript/math), [Number](/types/number)
2016-03-26 00:47:20 +01:00