task_url
stringlengths
30
116
task_name
stringlengths
2
86
task_description
stringlengths
0
14.4k
language_url
stringlengths
2
53
language_name
stringlengths
1
52
code
stringlengths
0
61.9k
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Phixmonti
Phixmonti
6 var a 3 var b   def tab 9 tochar print enddef   def printBits 8 int>bit reverse print nl enddef   a print " = " print tab a printBits b print " = " print tab b printBits tab "------------------------" print nl "AND = " print tab a b bitand printBits "OR = " print tab a b bitor printBits "XOR = " print tab a ...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Logo
Logo
to bsearch :value :a :lower :upper if :upper < :lower [output []] localmake "mid int (:lower + :upper) / 2 if item :mid :a > :value [output bsearch :value :a :lower :mid-1] if item :mid :a < :value [output bsearch :value :a :mid+1 :upper] output :mid end
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Free_Pascal
Free Pascal
program binaryDigits(input, output, stdErr); {$mode ISO}   function binaryNumber(const value: nativeUInt): shortString; const one = '1'; var representation: shortString; begin representation := binStr(value, bitSizeOf(value)); // strip leading zeroes, if any; NB: mod has to be ISO compliant delete(representation, ...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#PHP
PHP
function bitwise($a, $b) { function zerofill($a,$b) { if($a>=0) return $a>>$b; if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1); // this line shifts a 0 into the sign bit for compatibility, replace with "if($b==0) return $a;" if you need $b=0 to mean that nothing happens return ((~$a)>>...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Lolcode
Lolcode
  HAI 1.2 CAN HAS STDIO?   VISIBLE "HAI WORLD!!!1!" VISIBLE "IMA GONNA SHOW U BINA POUNCE NAO"   I HAS A list ITZ A BUKKIT list HAS A index0 ITZ 2 list HAS A index1 ITZ 3 list HAS A index2 ITZ 5 list HAS A index3 ITZ 7 list HAS A index4 ITZ 8 list HAS A index5 ITZ 9 list HAS A index6 ITZ 12 list...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#FreeBASIC
FreeBASIC
  ' FreeBASIC v1.05.0 win64 Dim As String fmt = "#### -> &" Print Using fmt; 5; Bin(5) Print Using fmt; 50; Bin(50) Print Using fmt; 9000; Bin(9000) Print Print "Press any key to exit the program" Sleep End  
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#PicoLisp
PicoLisp
: (& 6 3) -> 2   : (& 7 3 1) -> 1
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Lua
Lua
function binarySearch (list,value) local low = 1 local high = #list while low <= high do local mid = math.floor((low+high)/2) if list[mid] > value then high = mid - 1 elseif list[mid] < value then low = mid + 1 else return mid end end return false end
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Frink
Frink
  9000 -> binary 9000 -> base2 base2[9000] base[9000, 2]  
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Pike
Pike
  void bitwise(int a, int b) { write("a and b: %d\n", a & b); write("a or b:  %d\n", a | b); write("a xor b: %d\n", a ^ b); write("not a:  %d\n", ~a); write("a << b: 0x%x\n", a << b); write("a >> b:  %d\n", a >> b); // ints in Pike do not overflow, if a particular size of the int // is...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#M2000_Interpreter
M2000 Interpreter
  \\ binary search const N=10 Dim A(0 to N-1) A(0):=1,2,3,4,5,6,8,9,10,11 Print Len(A())=10 Function BinarySearch(&A(), aValue) { def long mid, lo, hi def boolean ok=False let lo=0, hi=Len(A())-1 While lo<=hi mid=(lo+hi)/2 if A(mid)>aValue Then hi=mid-1 Else.if A(mid)<aValue Then lo=mid+1 Else =mid...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#FunL
FunL
for n <- [5, 50, 9000, 9000000000] println( n, bin(n) )
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#PL.2FI
PL/I
/* PL/I can perform bit operations on binary integers. */ k = iand(i,j); k = ior(i,j); k = inot(i,j); k = ieor(i,j); k = isll(i,n); /* unsigned shifts i left by n places. */ k = isrl(i,n); /* unsigned shifts i right by n places. */ k = lower2(i, n); /* arithmetic right shift i by n places. */ k = raise2(i, n); /* arit...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#M4
M4
define(`notfound',`-1')dnl define(`midsearch',`ifelse(defn($1[$4]),$2,$4, `ifelse(eval(defn($1[$4])>$2),1,`binarysearch($1,$2,$3,decr($4))',`binarysearch($1,$2,incr($4),$5)')')')dnl define(`binarysearch',`ifelse(eval($4<$3),1,notfound,`midsearch($1,$2,$3,eval(($3+$4)/2),$4)')')dnl dnl define(`setrange',`ifelse(`$3',`',...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Futhark
Futhark
  fun main(x: i32): i64 = loop (out = 0i64) = for i < 32 do let digit = (x >> (31-i)) & 1 let out = (out * 10i64) + i64(digit) in out in out  
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Pop11
Pop11
define bitwise(a, b); printf(a && b, 'a and b = %p\n'); printf(a || b, 'a or b = %p\n'); printf(a ||/& b, 'a xor b = %p\n'); printf(~~ a, 'not a = %p\n'); printf(a << b, 'left shift of a by b = %p\n'); printf(a >> b, 'arithmetic right shift of a by b = %p\n'); enddefine;
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Maple
Maple
BinarySearch := proc( A, value, low, high ) description "recursive binary search"; if high < low then FAIL else local mid := iquo( high + low, 2 ); if A[ mid ] > value then thisproc( A, value, low, mid - 1 ) ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Gambas
Gambas
Public Sub Main() Dim siBin As Short[] = [5, 50, 9000] Dim siCount As Short   For siCount = 0 To siBin.Max Print Bin(siBin[siCount]) Next   End
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#PowerShell
PowerShell
$X -band $Y $X -bor $Y $X -bxor $Y -bnot $X
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Mathematica_.2F_Wolfram_Language
Mathematica / Wolfram Language
BinarySearchRecursive[x_List, val_, lo_, hi_] := Module[{mid = lo + Round@((hi - lo)/2)}, If[hi < lo, Return[-1]]; Return[ Which[x[[mid]] > val, BinarySearchRecursive[x, val, lo, mid - 1], x[[mid]] < val, BinarySearchRecursive[x, val, mid + 1, hi], True, mid] ]; ]
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Go
Go
package main   import ( "fmt" )   func main() { for i := 0; i < 16; i++ { fmt.Printf("%b\n", i) } }
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#PureBasic
PureBasic
Procedure Bitwise(a, b) Debug a & b ; And Debug a | b ;Or Debug a ! b ; XOr Debug ~a ;Not Debug a << b ; shift left Debug a >> b ; arithmetic shift right ; Logical shift right and rotates are not available ; You can of use inline ASM to achieve this: Define Temp ...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#MATLAB
MATLAB
function mid = binarySearchRec(list,value,low,high)   if( high < low ) mid = []; return end   mid = floor((low + high)/2);   if( list(mid) > value ) mid = binarySearchRec(list,value,low,mid-1); return elseif( list(mid) < value ) mid = binarySearchRec(list,valu...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Groovy
Groovy
print ''' n binary ----- --------------- ''' [5, 50, 9000].each { printf('%5d %15s\n', it, Integer.toBinaryString(it)) }
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Python
Python
def bitwise_built_ins(width, a, b): mask = (1 << width) - 1 print(f"""\ AND: 0b{a :0{width}b} & 0b{b :0{width}b} = 0b{(a & b) & mask :0{width}b}   OR: 0b{a :0{width}b} | 0b{b :0{width}b} = 0b{(a | b) & mask :0{width}b}   XOR: 0b{a :0{width...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Maxima
Maxima
find(L, n) := block([i: 1, j: length(L), k, p], if n < L[i] or n > L[j] then 0 else ( while j - i > 0 do ( k: quotient(i + j, 2), p: L[k], if n < p then j: k - 1 elseif n > p then i: k + 1 else i: j: k ), if n = L[i] then i else 0 ) )$   ".."(a, b) := ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Haskell
Haskell
import Data.List import Numeric import Text.Printf   -- Use the built-in function showIntAtBase. toBin n = showIntAtBase 2 ("01" !!) n ""   -- Implement our own version. toBin1 0 = [] toBin1 x = (toBin1 $ x `div` 2) ++ (show $ x `mod` 2)   -- Or even more efficient (due to fusion) and universal implementation toBin2 =...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#QB64
QB64
  ' no rotations and shift aritmetic are available in QB64 ' Bitwise operator in Qbasic and QB64 'AND (operator) the bit is set when both bits are set. 'EQV (operator) the bit is set when both are set or both are not set. 'IMP (operator) the bit is set when both are set or both are unset or the second condition bit is ...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#MAXScript
MAXScript
fn binarySearchIterative arr value = ( lower = 1 upper = arr.count while lower <= upper do ( mid = (lower + upper) / 2 if arr[mid] > value then ( upper = mid - 1 ) else if arr[mid] < value then ( lower = mid + 1 ) el...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Icon_and_Unicon
Icon and Unicon
procedure main() every i := 5 | 50 | 255 | 1285 | 9000 do write(i," = ",binary(i)) end   procedure binary(n) #: return bitstring for integer n static CT, cm, cb initial { CT := table() # cache table for results cm := 2 ^ (cb := 4) # (tunable) cache ...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Quackery
Quackery
[ [] swap 64 times [ 2 /mod number$ rot join swap ] drop echo$ cr ] is echobin ( n --> )   [ 64 swap - rot64 ] is rrot64 ( n --> n )   [ say "first integer: " over echobin say "second integer: " dup ...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#MiniScript
MiniScript
binarySearch = function(A, value, low, high) if high < low then return null mid = floor((low + high) / 2) if A[mid] > value then return binarySearch(A, value, low, mid-1) if A[mid] < value then return binarySearch(A, value, mid+1, high) return mid end function
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Idris
Idris
module Main   binaryDigit : Integer -> Char binaryDigit n = if (mod n 2) == 1 then '1' else '0'   binaryString : Integer -> String binaryString 0 = "0" binaryString n = pack (loop n []) where loop : Integer -> List Char -> List Char loop 0 acc = acc loop n acc = loop (div n 2) (binaryDigit n :: acc)  ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#J
J
tobin=: -.&' '@":@#: tobin 5 101 tobin 50 110010 tobin 9000 10001100101000
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#R
R
# Since R 3.0.0, the base package provides bitwise operators, see ?bitwAnd   a <- 35 b <- 42 bitwAnd(a, b) bitwOr(a, b) bitwXor(a, b) bitwNot(a) bitwShiftL(a, 2) bitwShiftR(a, 2)
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#N.2Ft.2Froff
N/t/roff
.de end .. .de array . nr \\$1.c 0 1 . de \\$1.push end . nr \\$1..\\\\n+[\\$1.c] \\\\$1 . end . de \\$1.pushln end . if \\\\n(.$>0 .\\$1.push \\\\$1 . if \\\\n(.$>1 \{ \ . shift . \\$1.pushln \\\\$@ \} . end .. . .de binarysearch . nr min 1 . nr max \\n[\\$1.c] . nr guess \\n[min]+\\n[max]/2 . while !\\n[\\$1.....
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Java
Java
public class Main { public static void main(String[] args) { System.out.println(Integer.toBinaryString(5)); System.out.println(Integer.toBinaryString(50)); System.out.println(Integer.toBinaryString(9000)); } }
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Racket
Racket
  #lang racket (define a 255) (define b 5) (list (bitwise-and a b) (bitwise-ior a b) (bitwise-xor a b) (bitwise-not a) (arithmetic-shift a b)  ; left shift (arithmetic-shift a (- b))) ; right shift  
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Nim
Nim
import algorithm   let s = @[2,3,4,5,6,7,8,9,10,12,14,16,18,20,22,25,27,30] echo binarySearch(s, 10)
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#JavaScript
JavaScript
function toBinary(number) { return new Number(number) .toString(2); } var demoValues = [5, 50, 9000]; for (var i = 0; i < demoValues.length; ++i) { // alert() in a browser, wscript.echo in WSH, etc. print(toBinary(demoValues[i])); }
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Raku
Raku
constant MAXINT = uint.Range.max; constant BITS = MAXINT.base(2).chars;   # define rotate ops for the fun of it multi sub infix:<⥁>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).list.rotate(b).reverse] } multi sub infix:<⥀>(Int:D \a, Int:D \b) { :2[(a +& MAXINT).polymod(2 xx BITS-1).reverse.list.rotate(b)...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Niue
Niue
1 2 3 4 5 3 bsearch . ( => 2 ) 5 bsearch . ( => 0 ) 'sam 'tom 'kenny ( must be sorted before calling bsearch ) sort .s ( => kenny sam tom ) 'sam bsearch . ( => 1 ) 'tom bsearch . ( => 0 ) 'kenny bsearch . ( => 2 ) 'tony bsearch . ( => -1)
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Joy
Joy
HIDE _ == [null] [pop] [2 div swap] [48 + putch] linrec IN int2bin == [null] [48 + putch] [_] ifte '\n putch END
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#jq
jq
def binary_digits: [ recurse( ./2 | floor; . > 0) % 2 ] | reverse | join("") ;   # The task: (5, 50, 9000) | binary_digits
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Red
Red
Red [Source: https://github.com/vazub/rosetta-red]   a: 10 b: 2   print [ pad "a =" 10 a newline pad "b =" 10 b newline pad "a AND b:" 10 a and b newline pad "a OR b:" 10 a or b newline pad "a XOR b:" 10 a xor b newline pad "NOT a:" 10 complement a newline pad "a >>> b:" 10 a >>> b newline pad "a >> b:" 10 a >>...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Objeck
Objeck
use Structure;   bundle Default { class BinarySearch { function : Main(args : String[]) ~ Nil { values := [-1, 3, 8, 13, 22]; DoBinarySearch(values, 13)->PrintLine(); DoBinarySearch(values, 7)->PrintLine(); }   function : native : DoBinarySearch(values : Int[], value : Int) ~ Int { ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Julia
Julia
using Printf   for n in (0, 5, 50, 9000) @printf("%6i → %s\n", n, string(n, base=2)) end   # with pad println("\nwith pad") for n in (0, 5, 50, 9000) @printf("%6i → %s\n", n, string(n, base=2, pad=20)) end
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Retro
Retro
  : bitwise ( ab- ) cr over "a = %d\n" puts dup "b = %d\n" puts 2over and "a and b = %d\n" puts 2over or "a or b = %d\n" puts 2over xor "a xor b = %d\n" puts over not "not a = %d\n" puts 2over << "a << b = %d\n" puts 2over >> "a >> b = %d\n" puts 2drop ;
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Objective-C
Objective-C
#import <Foundation/Foundation.h>   @interface NSArray (BinarySearch) // Requires all elements of this array to implement a -compare: method which // returns a NSComparisonResult for comparison. // Returns NSNotFound when not found - (NSInteger) binarySearch:(id)key; @end   @implementation NSArray (BinarySearch) - (NSI...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#K
K
tobin: ,/$2_vs tobin' 5 50 9000 ("101" "110010" "10001100101000")
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#REXX
REXX
╔═══════════════════════════════════════════════════════════════════════════════════════╗ ║ Since REXX stores numbers (indeed, all values) as characters, it makes no sense to ║ ║ "rotate" a value, since there aren't any boundaries for the value. I.E.: there ║ ║ isn't any 32─bit word "container" or "cel...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#OCaml
OCaml
let rec binary_search a value low high = if high = low then if a.(low) = value then low else raise Not_found else let mid = (low + high) / 2 in if a.(mid) > value then binary_search a value low (mid - 1) else if a.(mid) < value then binary_search a value (mid + 1) high el...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Kotlin
Kotlin
// version 1.0.5-2   fun main(args: Array<String>) { val numbers = intArrayOf(5, 50, 9000) for (number in numbers) println("%4d".format(number) + " -> " + Integer.toBinaryString(number)) }
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Ring
Ring
  x = 8 y = 2   see "x & y - Binary AND : " + (x & y) + nl see "x | y - Binary OR : " + (x | y) + nl see "x ^ y - Binary XOR : " + (x ^ y) +nl see "~x - Binary Ones Complement : " + (~x) + nl see "x << y - Binary Left Shift : " + (x << y) + nl see "x >> y - Binary Right Shift : " + (x >> y) + nl  
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Octave
Octave
function i = binsearch_r(array, val, low, high) if ( high < low ) i = 0; else mid = floor((low + high) / 2); if ( array(mid) > val ) i = binsearch_r(array, val, low, mid-1); elseif ( array(mid) < val ) i = binsearch_r(array, val, mid+1, high); else i = mid; endif endif e...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Lambdatalk
Lambdatalk
  {def dec2bin {lambda {:dec} {if {= :dec 0} then 0 else {if {< :dec 2} then 1 else {dec2bin {floor {/ :dec 2}}}{% :dec 2} }}}} -> dec2bin   {dec2bin 5} -> 101 {dec2bin 5} -> 110010 {dec2bin 9000} -> 10001100101000   {S.map dec2bin 5 50 9000} -> 101 110010 10001100101000   {S.map {lambda {:i} {br}...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#RLaB
RLaB
>> x = int(3); >> y = int(1); >> z = x && y; printf("0x%08x\n",z); // logical 'and' 0x00000001 >> z = x || y; printf("0x%08x\n",z); // logical 'or' 0x00000003 >> z = !x; printf("0x%08x\n",z); // logical 'not' 0xfffffffc >> i2 = int(2); >> z = x * i2; printf("0x%08x\n",z); // left-shift is multiplication by 2 where...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Ol
Ol
  (define (binary-search value vector) (let helper ((low 0) (high (- (vector-length vector) 1))) (unless (< high low) (let ((middle (quotient (+ low high) 2))) (cond ((> (vector-ref vector middle) value) (helper low (- middle 1))) ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Lang5
Lang5
'%b '__number_format set [5 50 9000] [3 1] reshape .
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Robotic
Robotic
  input string "First value" set "local1" to "input" input string "Second value" set "local2" to "input"   . ">>> is an arithmetic shift; >> is a logical shift" [ "a AND b = ('local1' a 'local2')" [ "a OR b = ('local1' o 'local2')" [ "a XOR b = ('local1' x 'local2')" [ "NOT a = (~'local1')" [ "a << b = ('local1' << 'lo...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#ooRexx
ooRexx
  data = .array~of(1, 3, 5, 7, 9, 11) -- search keys with a number of edge cases searchkeys = .array~of(0, 1, 4, 7, 11, 12) say "recursive binary search" loop key over searchkeys pos = recursiveBinarySearch(data, key) if pos == 0 then say "Key" key "not found" else say "Key" key "found at postion" pos end s...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#LFE
LFE
  (: io format '"~.2B~n~.2B~n~.2B~n" (list 5 50 9000))  
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Ruby
Ruby
def bitwise(a, b) form = "%1$7s:%2$6d  %2$016b" puts form % ["a", a] puts form % ["b", b] puts form % ["a and b", a & b] puts form % ["a or b ", a | b] puts form % ["a xor b", a ^ b] puts form % ["not a ", ~a] puts form % ["a << b ", a << b] # left shift puts form % ["a >> b ", a >> b] # arithmetic...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Oz
Oz
declare fun {BinarySearch Arr Val} fun {Search Low High} if Low > High then nil else Mid = (Low+High) div 2 in if Val < Arr.Mid then {Search Low Mid-1} elseif Val > Arr.Mid then {Search Mid+1 High} else [Mid] end end end ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Liberty_BASIC
Liberty BASIC
for a = 0 to 16 print a;"=";dec2bin$(a) next a=50:print a;"=";dec2bin$(a) a=254:print a;"=";dec2bin$(a) a=9000:print a;"=";dec2bin$(a) wait   function dec2bin$(num) if num=0 then dec2bin$="0":exit function while num>0 dec2bin$=str$(num mod 2)+dec2bin$ num=int(num/2) wend end function  
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Rust
Rust
fn main() { let a: u8 = 105; let b: u8 = 91; println!("a = {:0>8b}", a); println!("b = {:0>8b}", b); println!("a | b = {:0>8b}", a | b); println!("a & b = {:0>8b}", a & b); println!("a ^ b = {:0>8b}", a ^ b); println!("!a = {:0>8b}", !a); println!("a << 3 = {:0>8b}",...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#PARI.2FGP
PARI/GP
setsearch(s, n)
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Little_Man_Computer
Little Man Computer
  // Little Man Computer, for Rosetta Code. // Read numbers from user and display them in binary. // Exit when input = 0. input INP BRZ zero STA N // Write number followed by '->' OUT LDA asc_hy OTC LDA asc_gt OTC // Find greatest power of 2 not exceedin...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#SAS
SAS
/* rotations are not available, but are easy to implement with the other bitwise operators */ data _null_; a=105; b=91; c=bxor(a,b); d=band(a,b); e=bor(a,b); f=bnot(a); /* on 32 bits */ g=blshift(a,1); h=brshift(a,1); put _all_; run;
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Pascal
Pascal
function binary_search(element: real; list: array of real): integer; var l, m, h: integer; begin l := Low(list); h := High(list); binary_search := -1; while l <= h do begin m := (l + h) div 2; if list[m] > element then begin h := m - 1; end els...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#LLVM
LLVM
; ModuleID = 'binary.c' ; source_filename = "binary.c" ; target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" ; target triple = "x86_64-pc-windows-msvc19.21.27702"   ; This is not strictly LLVM, as it uses the C library function "printf". ; LLVM does not provide a way to print values, so the alternative would be...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Scala
Scala
def bitwise(a: Int, b: Int) { println("a and b: " + (a & b)) println("a or b: " + (a | b)) println("a xor b: " + (a ^ b)) println("not a: " + (~a)) println("a << b: " + (a << b)) // left shift println("a >> b: " + (a >> b)) // arithmetic right shift println("a >>> b: " + (a >>> b)) // unsigned right shift...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Perl
Perl
sub binary_search { my ($array_ref, $value, $left, $right) = @_; while ($left <= $right) { my $middle = int(($right + $left) >> 1); if ($value == $array_ref->[$middle]) { return $middle; } elsif ($value < $array_ref->[$middle]) { $right = $middle - 1; ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Locomotive_Basic
Locomotive Basic
10 PRINT BIN$(5) 20 PRINT BIN$(50) 30 PRINT BIN$(9000)
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Scheme
Scheme
(import (rnrs arithmetic bitwise (6)))   (define (bitwise a b) (display (bitwise-and a b)) (newline) (display (bitwise-ior a b)) (newline) (display (bitwise-xor a b)) (newline) (display (bitwise-not a)) (newline) (display (bitwise-arithmetic-shift-right a b)) (newline))   (bitwise 255 5)
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Phix
Phix
global function binary_search(object needle, sequence haystack) integer lo = 1, hi = length(haystack), mid = lo, c = 0 while lo<=hi do mid = floor((lo+hi)/2) c = compare(needle, haystack[mid]) if c<0 then hi = mid-1 elsif c>0 then lo ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#LOLCODE
LOLCODE
HAI 1.3 HOW IZ I DECIMULBINUR YR DECIMUL I HAS A BINUR ITZ "" IM IN YR DUUH BOTH SAEM DECIMUL AN SMALLR OF DECIMUL AN 0, O RLY? YA RLY, GTFO OIC BINUR R SMOOSH MOD OF DECIMUL AN 2 BINUR MKAY DECIMUL R MAEK QUOSHUNT OF DECIMUL AN 2 A NUMBR IM OUTTA YR DUUH FOUND YR BINUR IF U SAY SO VISIBLE...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Seed7
Seed7
$ include "seed7_05.s7i"; include "bin32.s7i";   const proc: bitwise (in integer: a, in integer: b) is func begin writeln("a: " <& a radix 2 lpad0 32); writeln("b: " <& b radix 2 lpad0 32); writeln("integer operations:"); writeln("a << b: " <& a << b radix 2 lpad0 32); # left shif...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#PHP
PHP
function binary_search( $array, $secret, $start, $end ) { do { $guess = (int)($start + ( ( $end - $start ) / 2 ));   if ( $array[$guess] > $secret ) $end = $guess;   if ( $array[$guess] < $secret ) $start = $...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Lua
Lua
function dec2bin (n) local bin = "" while n > 0 do bin = n % 2 .. bin n = math.floor(n / 2) end return bin end   print(dec2bin(5)) print(dec2bin(50)) print(dec2bin(9000))
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Sidef
Sidef
func bitwise(a, b) { say ('a and b : ', a & b) say ('a or b  : ', a | b) say ('a xor b : ', a ^ b) say ('not a  : ', ~a) say ('a << b  : ', a << b) # left shift say ('a >> b  : ', a >> b) # arithmetic right shift }   bitwise(14,3)
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Picat
Picat
go => A = [2, 4, 6, 8, 9], TestValues = [2,1,8,10,9,5],   foreach(Value in TestValues) test(binary_search,A, Value) end, test(binary_search,[1,20,3,4], 5), nl.   % Test with binary search predicate Search test(Search,A,Value) => Ret = apply(Search,A,Value), printf("A: %w Value:%d Ret: %d: ", A, Val...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#M2000_Interpreter
M2000 Interpreter
  Module Checkit { Form 90, 40 Function BinFunc${ Dim Base 0, One$(16) One$( 0 ) = "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" =lambda$ One$() (x, oct as long=4, bypass as boolean=True) ...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Simula
Simula
BEGIN COMMENT TO MY KNOWLEDGE SIMULA DOES NOT SUPPORT BITWISE OPERATIONS SO WE MUST WRITE PROCEDURES FOR THE JOB ; INTEGER WORDSIZE; WORDSIZE := 32; BEGIN   PROCEDURE TOBITS(N,B); INTEGER N; BOOLEAN ARRAY B; BEGIN INTEGER I,BITN; FOR I := WORDSIZE-1 STEP -1 UNTIL 0 DO BEGIN ...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#PicoLisp
PicoLisp
(de recursiveSearch (Val Lst Len) (unless (=0 Len) (let (N (inc (/ Len 2)) L (nth Lst N)) (cond ((= Val (car L)) Val) ((> Val (car L)) (recursiveSearch Val (cdr L) (- Len N)) ) (T (recursiveSearch Val Lst (dec N))) ) ) ) )
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#MAD
MAD
NORMAL MODE IS INTEGER   INTERNAL FUNCTION(NUM) ENTRY TO BINARY. BTEMP = NUM BRSLT = 0 BDIGIT = 1 BIT WHENEVER BTEMP.NE.0 BRSLT = BRSLT + BDIGIT * (BTEMP-BTEMP/2*2) BTEMP = BTEMP/2 BDIGIT = BD...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Slate
Slate
[ |:a :b |   inform: (a bitAnd: b) printString. inform: (a bitOr: b) printString. inform: (a bitXor: b) printString. inform: (a bitNot) printString. inform: (a << b) printString. inform: (a >> b) printString.   ] applyTo: {8. 12}.
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#PL.2FI
PL/I
/* A binary search of list A for element M */ search: procedure (A, M) returns (fixed binary); declare (A(*), M) fixed binary; declare (l, r, mid) fixed binary;   l = lbound(a,1)-1; r = hbound(A,1)+1; do while (l <= r); mid = (l+r)/2; if A(mid) = M then return (mid); if A(mid) < M then ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Maple
Maple
  > convert( 50, 'binary' ); 110010 > convert( 9000, 'binary' ); 10001100101000  
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Mathematica_.2F_Wolfram_Language
Mathematica / Wolfram Language
StringJoin @@ ToString /@ IntegerDigits[50, 2]
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Smalltalk
Smalltalk
| testBitFunc | testBitFunc := [ :a :b | ('%1 and %2 is %3' % { a. b. (a bitAnd: b) }) displayNl. ('%1 or %2 is %3' % { a. b. (a bitOr: b) }) displayNl. ('%1 xor %2 is %3' % { a. b. (a bitXor: b) }) displayNl. ('not %1 is %2' % { a. (a bitInvert) }) displayNl. ('%1 left shift %2 is %3' % { a. b...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Pop11
Pop11
define BinarySearch(A, value); lvars low = 1, high = length(A), mid; while low <= high do (low + high) div 2 -> mid; if A(mid) > value then mid - 1 -> high; elseif A(mid) < value then mid + 1 -> low; else return(mid); endif; endwhil...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#MATLAB_.2F_Octave
MATLAB / Octave
dec2bin(5) dec2bin(50) dec2bin(9000)
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Standard_ML
Standard ML
fun bitwise_ints (a, b) = ( print ("a and b: " ^ IntInf.toString (IntInf.andb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n"); print ("a or b: " ^ IntInf.toString (IntInf.orb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\n"); print ("a xor b: " ^ IntInf.toString (IntInf.xorb (IntInf.fromInt a, IntInf.fromInt b)) ^ "\...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#PowerShell
PowerShell
  function BinarySearch-Iterative ([int[]]$Array, [int]$Value) { [int]$low = 0 [int]$high = $Array.Count - 1   while ($low -le $high) { [int]$mid = ($low + $high) / 2   if ($Array[$mid] -gt $Value) { $high = $mid - 1 } elseif ($Array[$mid] -lt $Value) ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#Maxima
Maxima
digits([arg]) := block( [n: first(arg), b: if length(arg) > 1 then second(arg) else 10, v: [ ], q], do ( [n, q]: divide(n, b), v: cons(q, v), if n=0 then return(v)))$   binary(n) := simplode(digits(n, 2))$ binary(9000); /* 10001100101000 */
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Stata
Stata
func bitwise(a: Int, b: Int) { // All bitwise operations (including shifts) // require both operands to be the same type println("a AND b: \(a & b)") println("a OR b: \(a | b)") println("a XOR b: \(a ^ b)") println("NOT a: \(~a)") println("a << b: \(a << b)") // left shift // for right shifts, if the op...
http://rosettacode.org/wiki/Binary_search
Binary search
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm. As an analogy, consider the children's game "guess a number." The scorer has a secret number, and will only tell the p...
#Prolog
Prolog
bin_search(Elt,List,Result):- length(List,N), bin_search_inner(Elt,List,1,N,Result).   bin_search_inner(Elt,List,J,J,J):- nth(J,List,Elt). bin_search_inner(Elt,List,Begin,End,Mid):- Begin < End, Mid is (Begin+End) div 2, nth(Mid,List,Elt). bin_search_inner(Elt,List,Begin,End,Result):- Begin < End, Mid is ...
http://rosettacode.org/wiki/Binary_digits
Binary digits
Task Create and display the sequence of binary digits for a given   non-negative integer. The decimal value   5   should produce an output of   101 The decimal value   50   should produce an output of   110010 The decimal value   9000   should produce an output of   10001100101000 ...
#MAXScript
MAXScript
  -- MAXScript: Output decimal numbers from 0 to 16 as Binary : N.H. 2019 for k = 0 to 16 do ( temp = "" binString = "" b = k -- While loop wont execute for zero so force string to zero if b == 0 then temp = "0" while b > 0 do ( rem = b b = b / 2 If ((mod rem 2) as Integer) == 0 then temp = temp + "0" els...
http://rosettacode.org/wiki/Bitwise_operations
Bitwise operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Swift
Swift
func bitwise(a: Int, b: Int) { // All bitwise operations (including shifts) // require both operands to be the same type println("a AND b: \(a & b)") println("a OR b: \(a | b)") println("a XOR b: \(a ^ b)") println("NOT a: \(~a)") println("a << b: \(a << b)") // left shift // for right shifts, if the op...