pxt-calliope/olddocs/js/bits.md

68 lines
2.3 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.
2016-04-13 17:27:45 +02:00
The Bits library includes functions for bit-level manipulation of integers. In the [Touch Develop editor](/js/editor), click `bits` to see the following bit functions:
2016-03-26 00:47:20 +01:00
## Bitwise and, or, and xor functions
#### Syntax
2016-04-13 17:27:45 +02:00
bits `->` *and/or/xor* uint32 (x : [Number](/reference/types/number), y : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
#### Parameters
2016-04-13 17:27:45 +02:00
* x - an unsigned 32 bit integer [Number](/reference/types/number)
* y - another unsigned 32 bit integer [Number](/reference/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
2016-04-13 17:27:45 +02:00
bits `->` rotate left unint32 (x : [Number](/reference/types/number), bits : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
bits `->` rotate right unint32 (x : [Number](/reference/types/number), bits : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
#### Parameters
2016-04-13 17:27:45 +02:00
* x - [Number](/reference/types/number);
* bits - [Number](/reference/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
2016-04-13 17:27:45 +02:00
bits `->` shift left unint32 (x : [Number](/reference/types/number), bits : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
bits `->` shift right unint32 (x : [Number](/reference/types/number), bits : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00
#### Parameters
2016-04-13 17:27:45 +02:00
* x - [Number](/reference/types/number);
* bits - [Number](/reference/types/number);
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[statements and operators](/js/statements), [math functions](/js/math), [Number](/reference/types/number)
2016-03-26 00:47:20 +01:00