Difference between revisions of "Bit.brshift"

From ComputerCraft Wiki
Jump to: navigation, search
m (Int linking fix.)
(Explain that this is an arithmetic, not logical, shift, and add a corresponding example)
Line 4: Line 4:
 
|args=[[int (type)|int]] n, [[int (type)|int]] bits
 
|args=[[int (type)|int]] n, [[int (type)|int]] bits
 
|api=bit
 
|api=bit
|returns=[[int (type)|int]] the value of <var>n</var> shifted right by <var>bits</var> bits, which is equivalent to ⌊<var>n</var>÷2<sup><var>bits</var></sup>⌋
+
|returns=[[int (type)|int]] the value of <var>n</var> shifted right by <var>bits</var> bits, with the shifted-in bits being equal to the original number’s 31st bit, which is equivalent to ⌊<var>n</var>÷2<sup><var>bits</var></sup>⌋
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Shifts a number right by a specified number of bits
+
|desc=Shifts a number right arithmetically by a specified number of bits
 
|examples=
 
|examples=
 
{{Example
 
{{Example
Line 13: Line 13:
 
|output=18
 
|output=18
 
}}
 
}}
 +
{{Example
 +
|desc=Shift the number 2,147,483,648 (10000000000000000000000000000000) right by 2 bits, yielding 3,758,096,384 (11100000000000000000000000000000)
 +
|code=print(bit.brshift(2147483648, 2))
 +
|output=3758096384
 
}}
 
}}
  
 
[[Category:API_Functions]]
 
[[Category:API_Functions]]

Revision as of 18:08, 21 April 2013

{{Function |name=bit.brshift |args=int n, int bits |api=bit |returns=int the value of n shifted right by bits bits, with the shifted-in bits being equal to the original number’s 31st bit, which is equivalent to ⌊n÷2bits⌋ |addon=ComputerCraft |desc=Shifts a number right arithmetically by a specified number of bits |examples=

Grid paper.png  Example
Shift the number 73 (1001001) right by 2 bits, yielding 18 (10010)
Code
print(bit.brshift(73, 2))
Output 18



Grid paper.png  Example
Shift the number 2,147,483,648 (10000000000000000000000000000000) right by 2 bits, yielding 3,758,096,384 (11100000000000000000000000000000)
Code
print(bit.brshift(2147483648, 2))
Output 3758096384