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/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... | #PureBasic | PureBasic | #Recursive = 0 ;recursive binary search method
#Iterative = 1 ;iterative binary search method
#NotFound = -1 ;search result if item not found
;Recursive
Procedure R_BinarySearch(Array a(1), value, low, high)
Protected mid
If high < low
ProcedureReturn #NotFound
EndIf
mid = (low + high) / 2
If a(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
... | #Mercury | Mercury | :- module binary_digits.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, list, string.
main(!IO) :-
list.foldl(print_binary_digits, [5, 50, 9000], !IO).
:- pred print_binary_digits(int::in, io::di, io::uo) is det.
print_binary_digits(N, !IO)... |
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
... | #min | min | (2 over over mod 'div dip) :divmod2
(
:n () =list
(n 0 >) (n divmod2 list append #list @n) while
list reverse 'string map "" join
"^0+" "" replace ;remove leading zeroes
) :bin
(5 50 9000) (bin puts) foreach |
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 |... | #SystemVerilog | SystemVerilog | program main;
initial begin
bit [52:0] a,b,c;
a = 53'h123476547890fe;
b = 53'h06453bdef23ca6;
c = a & b; $display("%h & %h = %h", a,b,c);
c = a | b; $display("%h | %h = %h", a,b,c);
c = a ^ b; $display("%h ^ %h = %h", a,b,c);
c = ~ a; $display("~%h = %h", a, c);
c = a << 5; $di... |
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... | #Python | Python | def binary_search(l, value):
low = 0
high = len(l)-1
while low <= high:
mid = (low+high)//2
if l[mid] > value: high = mid-1
elif l[mid] < value: low = mid+1
else: return mid
return -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
... | #MiniScript | MiniScript | binary = function(n)
result = ""
while n
result = str(n%2) + result
n = floor(n/2)
end while
if not result then return "0"
return result
end function
print binary(5)
print binary(50)
print binary(9000)
print binary(0) |
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
... | #mLite | mLite | fun binary
(0, b) = implode ` map (fn x = if int x then chr (x + 48) else x) b
| (n, b) = binary (n div 2, n mod 2 :: b)
| n = binary (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 |... | #Tailspin | Tailspin |
def a: [x f075 x];
def b: [x 81 x];
($a and $b) -> '$a; and $b; is $;$#10;' -> !OUT::write
($a or $b) -> '$a; or $b; is $;$#10;' -> !OUT::write
($a xor $b) -> '$a; xor $b; is $;$#10;' -> !OUT::write
$a::inverse -> 'not $a; is $;$#10;' -> !OUT::write
$a::shift&{left: 3, fill: [x 00 x]} -> '$a; shifted left 3 bits 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... | #Quackery | Quackery | [ stack ] is value.bs ( --> n )
[ stack ] is nest.bs ( --> n )
[ stack ] is test.bs ( --> n )
[ ]'[ test.bs put
value.bs put
nest.bs put
1 - swap
[ 2dup < if done
2dup + 1 >>
nest.bs shar... |
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
... | #Modula-2 | Modula-2 | MODULE Binary;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT Write,WriteLn,ReadChar;
PROCEDURE PrintByte(b : INTEGER);
VAR v : INTEGER;
BEGIN
v := 080H;
WHILE v#0 DO
IF (b BAND v) # 0 THEN
Write('1')
ELSE
Write('0')
END;
v := v SHR 1
EN... |
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 |... | #Tcl | Tcl | proc bitwise {a b} {
puts [format "a and b: %#08x" [expr {$a & $b}]]
puts [format "a or b: %#08x" [expr {$a | $b}]]
puts [format "a xor b: %#08x" [expr {$a ^ $b}]]
puts [format "not a: %#08x" [expr {~$a}]]
puts [format "a << b: %#08x" [expr {$a << $b}]]
puts [format "a >> b: %#08x" [expr {$... |
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... | #R | R | BinSearch <- function(A, value, low, high) {
if ( high < low ) {
return(NULL)
} else {
mid <- floor((low + high) / 2)
if ( A[mid] > value )
BinSearch(A, value, low, mid-1)
else if ( A[mid] < value )
BinSearch(A, value, mid+1, high)
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
... | #Modula-3 | Modula-3 | MODULE Binary EXPORTS Main;
IMPORT IO, Fmt;
VAR num := 10;
BEGIN
IO.Put(Fmt.Int(num, 2) & "\n");
num := 150;
IO.Put(Fmt.Int(num, 2) & "\n");
END Binary. |
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 |... | #TI-89_BASIC | TI-89 BASIC | bitwise(a,b)
Prgm
Local show, oldbase
Define show(label, x)=Prgm
Local r
setMode("Base","DEC")
string(x) → r
setMode("Base","HEX")
Disp label & r & " " & string(x)
EndPrgm
getMode("Base") → oldbase
show("", {a, b})
show("And ", a and b)
show("Or ", a or b)
show("Xor ", a xor b)
sh... |
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... | #Racket | Racket |
#lang racket
(define (binary-search x v)
; loop : index index -> index or #f
; return i s.t. l<=i<h and v[i]=x
(define (loop l h)
(cond [(>= l h) #f]
[else (define m (quotient (+ l h) 2))
(define y (vector-ref v m))
(cond
[(> y x) (loop l (- m 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
... | #NetRexx | NetRexx | /* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
method getBinaryDigits(nr) public static
bd = nr.d2x.x2b.strip('L', 0)
if bd.length = 0 then bd = 0
return bd
method runSample(arg) public static
parse arg list
if list = '' then list = '0 5 50 9000'
loo... |
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 |... | #Vala | Vala | void testbit(int a, int b) {
print(@"input: a = $a, b = $b\n");
print(@"AND: $a & $b = $(a & b)\n");
print(@"OR: $a | $b = $(a | b)\n");
print(@"XOR: $a ^ $b = $(a ^ b)\n");
print(@"LSH: $a << $b = $(a << b)\n");
print(@"RSH: $a >> $b = $(a >> b)\n");
print(@"NOT: ~$a = $(~a)\n");
/* there are... |
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... | #Raku | Raku | sub search (@a, $x --> Int) {
binary_search { $x cmp @a[$^i] }, 0, @a.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
... | #NewLisp | NewLisp |
;;; Using the built-in "bits" function
;;; For integers up to 9,223,372,036,854,775,807
(map println (map bits '(0 5 50 9000)))
;;; n > 0, "unlimited" size
(define (big-bits n)
(let (res "")
(while (> n 0)
(push (if (even? n) "0" "1") res)
(setq n (/ n 2)))
res))
;;; Example
(println (big-bits 12345678901... |
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 |... | #VBA | VBA | Debug.Print Hex(&HF0F0 And &HFF00) 'F000
Debug.Print Hex(&HF0F0 Or &HFF00) 'FFF0
Debug.Print Hex(&HF0F0 Xor &HFF00) 'FF0
Debug.Print Hex(Not &HF0F0) 'F0F
Debug.Print Hex(&HF0F0 Eqv &HFF00) 'F00F
Debug.Print Hex(&HF0F0 Imp &HFF00) 'FF0F
|
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... | #REXX | REXX | /*REXX program finds a value in a list of integers using an iterative binary search.*/
@= 3 7 13 19 23 31 43 47 61 73 83 89 103 109 113 131 139 151 167 181,
193 199 229 233 241 271 283 293 313 317 337 349 353 359 383 389 401 409 421 433,
443 449 463 467 491 503 509 523 547 571 577 601 619 643 647 6... |
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
... | #Nickle | Nickle | prompt$ nickle
> 0 # 2
0
> 5 # 2
101
> 50 # 2
110010
> 9000 # 2
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
... | #Nim | Nim | proc binDigits(x: BiggestInt, r: int): int =
## Calculates how many digits `x` has when each digit covers `r` bits.
result = 1
var y = x shr r
while y > 0:
y = y shr r
inc(result)
proc toBin*(x: BiggestInt, len: Natural = 0): string =
## converts `x` into its binary representation. The resulting str... |
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 |... | #Visual_Basic | Visual Basic | Sub Test(a as Integer, b as Integer)
WriteLine("And " & a And b)
WriteLine("Or " & a Or b)
WriteLine("Xor " & a Xor b)
WriteLine("Not " & Not a)
WriteLine("Left Shift " & a << 2)
WriteLine("Right Shift " & a >> 2)
End Sub |
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... | #Ring | Ring |
decimals(0)
array = [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
find= 42
index = where(array,find,0,len(array))
if index >= 0
see "the value " + find+ " was found at index " + index
else
see "the value " + find + " was not found"
ok
func where(a,s,b,t)
h = 2
while h<(t-b)
h *= 2
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
... | #Oberon-2 | Oberon-2 |
MODULE BinaryDigits;
IMPORT Out;
PROCEDURE OutBin(x: INTEGER);
BEGIN
IF x > 1 THEN OutBin(x DIV 2) END;
Out.Int(x MOD 2, 1);
END OutBin;
BEGIN
OutBin(0); Out.Ln;
OutBin(1); Out.Ln;
OutBin(2); Out.Ln;
OutBin(3); Out.Ln;
OutBin(42); Out.Ln;
END BinaryDigits.
|
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 |... | #Visual_Basic_.NET | Visual Basic .NET | Sub Test(a as Integer, b as Integer)
WriteLine("And " & a And b)
WriteLine("Or " & a Or b)
WriteLine("Xor " & a Xor b)
WriteLine("Not " & Not a)
WriteLine("Left Shift " & a << 2)
WriteLine("Right Shift " & a >> 2)
End Sub |
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... | #Ruby | Ruby | class Array
def binary_search(val, low=0, high=(length - 1))
return nil if high < low
mid = (low + high) >> 1
case val <=> self[mid]
when -1
binary_search(val, low, mid - 1)
when 1
binary_search(val, mid + 1, high)
else mid
end
end
end
ary = [0,1,4,5,6,7,8,9,12,26... |
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
... | #Objeck | Objeck | class Binary {
function : Main(args : String[]) ~ Nil {
5->ToBinaryString()->PrintLine();
50->ToBinaryString()->PrintLine();
9000->ToBinaryString()->PrintLine();
}
} |
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 |... | #Wren | Wren | var rl = Fn.new { |x, y| x << y | x >> (32-y) }
var rr = Fn.new { |x, y| x >> y | x << (32-y) }
var bitwise = Fn.new { |x, y|
if (!x.isInteger || !y.isInteger || x < 0 || y < 0 || x > 0xffffffff || y > 0xffffffff) {
Fiber.abort("Operands must be in the range of a 32-bit unsigned integer")
}
Syst... |
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... | #Run_BASIC | Run BASIC | dim theArray(100)
global theArray
for i = 1 to 100
theArray(i) = i
next i
print binarySearch(80,30,90)
FUNCTION binarySearch(val, lo, hi)
IF hi < lo THEN
binarySearch = 0
ELSE
middle = (hi + lo) / 2
if val < theArray(middle) then binarySearch = binarySearch(val, lo, middle-1)
if val > theArray... |
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
... | #OCaml | OCaml | let bin_of_int d =
if d < 0 then invalid_arg "bin_of_int" else
if d = 0 then "0" else
let rec aux acc d =
if d = 0 then acc else
aux (string_of_int (d land 1) :: acc) (d lsr 1)
in
String.concat "" (aux [] d)
let () =
let d = read_int () in
Printf.printf "%8s\n" (bin_of_int d) |
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 |... | #x86_Assembly | x86 Assembly | extern printf
global main
section .text
main
mov eax, dword [_a]
mov ecx, dword [_b]
push ecx
push eax
and eax, ecx
mov ebx, _opand
call out_ops
call get_nums
or eax, ecx
mov ebx, _opor
call out_ops
call get_nums
xor eax, ecx
mov ebx, _opxor
call out_ops
call get_nums
shr eax, cl
mov... |
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... | #Rust | Rust | fn binary_search<T:PartialOrd>(v: &[T], searchvalue: T) -> Option<T> {
let mut lower = 0 as usize;
let mut upper = v.len() - 1;
while upper >= lower {
let mid = (upper + lower) / 2;
if v[mid] == searchvalue {
return Some(searchvalue);
} else if searchvalue < v[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
... | #Oforth | Oforth | >5 asStringOfBase(2) println
101
ok
>50 asStringOfBase(2) println
110010
ok
>9000 asStringOfBase(2) println
10001100101000
ok
>423785674235000123456789 asStringOfBase(2) println
1011001101111010111011110101001101111000000000000110001100000100111110100010101
ok
|
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
... | #Ol | Ol |
(print (number->string 5 2))
(print (number->string 50 2))
(print (number->string 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 |... | #XBasic | XBasic |
PROGRAM "bitwise"
DECLARE FUNCTION Entry()
INTERNAL FUNCTION ULONG Rotr(ULONG x, ULONG s)
FUNCTION Entry()
SLONG a, b
ULONG ua, ub
a = 21
b = 3
ua = a
ub = b
PRINT
PRINT "= Decimal ="
PRINT LTRIM$(STR$(a)); " AND"; b; ":"; a & b ' also: a AND b
PRINT LTRIM$(STR$(a)); " OR"; b; ":"; a | b ' als... |
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... | #Scala | Scala | def binarySearch[A <% Ordered[A]](a: IndexedSeq[A], v: A) = {
def recurse(low: Int, high: Int): Option[Int] = (low + high) / 2 match {
case _ if high < low => None
case mid if a(mid) > v => recurse(low, mid - 1)
case mid if a(mid) < v => recurse(mid + 1, high)
case mid => Some(mid)
}
recurse(0, a.... |
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
... | #OxygenBasic | OxygenBasic |
function BinaryBits(sys n) as string
string buf=nuls 32
sys p=strptr buf
sys le
mov eax,n
mov edi,p
mov ecx,32
'
'STRIP LEADING ZEROS
(
dec ecx
jl fwd done
shl eax,1
jnc repeat
)
'PLACE DIGITS
'
mov byte [edi],49 '1'
inc edi
(
cmp ecx,0
jle exit
mov dl,48 '0'
sh... |
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
... | #Panda | Panda | 0..15.radix:2 nl |
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 |... | #XLISP | XLISP | (defun bitwise-operations (a b)
; rotate operations are not supported
(print `(,a and ,b = ,(logand a b)))
(print `(,a or ,b = ,(logior a b)))
(print `(,a xor ,b = ,(logxor a b)))
(print `(,a left shift by ,b = ,(lsh a b)))
(print `(,a right shift by ,b = ,(lsh a (- b)))) ; negative second operand shifts right
(... |
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... | #Scheme | Scheme | (define (binary-search value vector)
(let helper ((low 0)
(high (- (vector-length vector) 1)))
(if (< high low)
#f
(let ((middle (quotient (+ low high) 2)))
(cond ((> (vector-ref vector middle) value)
(helper low (- middle 1)))
((< (vector-... |
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
... | #PARI.2FGP | PARI/GP | bin(n:int)=concat(apply(s->Str(s),binary(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 |... | #XPL0 | XPL0 | Text(0, "A and B = "); HexOut(0, A and B); CrLf(0); \alternate symbol: &
Text(0, "A or B = "); HexOut(0, A or B); CrLf(0); \alternate symbol: !
Text(0, "A xor B = "); HexOut(0, A xor B); CrLf(0); \alternate symbol: |
Text(0, "not A = "); HexOut(0, not A); CrLf(0); \alternate symbol: ~
Text(0, "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... | #Seed7 | Seed7 | const func integer: binarySearchIterative (in array elemType: arr, in elemType: aKey) is func
result
var integer: result is 0;
local
var integer: low is 1;
var integer: high is 0;
var integer: middle is 0;
begin
high := length(arr);
while result = 0 and low <= high do
middle := low +... |
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
... | #Pascal | Pascal | program IntToBinTest;
{$MODE objFPC}
uses
strutils;//IntToBin
function WholeIntToBin(n: NativeUInt):string;
var
digits: NativeInt;
begin
// BSR?Word -> index of highest set bit but 0 -> 255 ==-1 )
IF n <> 0 then
Begin
{$ifdef CPU64}
digits:= BSRQWord(NativeInt(n))+1;
{$ELSE}
digits:= BSRDWord(NativeInt(... |
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 |... | #Yabasic | Yabasic | sub formBin$(n)
return right$("00000000" + bin$(n), 8)
end sub
a = 6 : b = 3
print a, " = \t", formBin$(a)
print b, " = \t", formBin$(b)
print "\t--------"
print "AND = \t", formBin$(and(a, b))
print "OR = \t", formBin$(or(a, b))
print "XOR = \t", formBin$(xor(a, b))
print "NOT ", a, " =\t", formBin$(xor(255, 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... | #SequenceL | SequenceL | binarySearch(A(1), value(0), low(0), high(0)) :=
let
mid := low + (high - low) / 2;
in
-1 when high < low //Not Found
else
binarySearch(A, value, low, mid - 1) when A[mid] > value
else
binarySearch(A, value, mid + 1, high) when A[mid] < value
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
... | #Peloton | Peloton | <@ defbaslit>2</@>
<@ saybaslit>0</@>
<@ saybaslit>5</@>
<@ saybaslit>50</@>
<@ saybaslit>9000</@>
|
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
... | #Perl | Perl | for (5, 50, 9000) {
printf "%b\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 |... | #Z80_Assembly | Z80 Assembly | LD A,&05
AND &1F ;0x05 & 0x1F |
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... | #Sidef | Sidef | func binary_search(a, i) {
var l = 0
var h = a.end
while (l <= h) {
var mid = (h+l / 2 -> int)
a[mid] > i && (h = mid-1; next)
a[mid] < i && (l = mid+1; next)
return mid
}
return -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
... | #Phix | Phix | printf(1,"%b\n",5)
printf(1,"%b\n",50)
printf(1,"%b\n",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 |... | #zkl | zkl | (7).bitAnd(1) //-->1
(8).bitOr(1) //-->9
(7).bitXor(1) //-->6
(1).bitNot() : "%,x".fmt(_) //-->ff|ff|ff|ff|ff|ff|ff|fe
(7).shiftRight(1) //-->3
(7).shiftLeft(1) //-->0xe
(-1).toString(16) //-->ffffffffffffffff
(-1).shiftRight(1).toString(16) //-->7fffffffffffffff |
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... | #Simula | Simula | BEGIN
INTEGER PROCEDURE BINARYSEARCHREC(A, LVALUE);
INTEGER ARRAY A;
INTEGER LVALUE; ! VALUE IS A KEY WORD ;
BEGIN
INTEGER PROCEDURE SEARCH(LOW, HIGH);
INTEGER LOW, HIGH;
BEGIN
INTEGER MID;
! INVARIANTS: VALUE > A[I] FOR ALL I < LOW
... |
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
... | #Phixmonti | Phixmonti | def printBinary
"The decimal value " print dup print " should produce an output of " print
20 int>bit
len 1 -1 3 tolist
for
get not
if
-1 del
else
exitfor
endif
endfor
len 1 -1 3 tolist
for
get print
endfor
nl
enddef
... |
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
... | #PHP | PHP | <?php
echo decbin(5);
echo decbin(50);
echo decbin(9000); |
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... | #SPARK | SPARK | package Binary_Searches is
subtype Item_Type is Integer; -- From specs.
subtype Index_Type is Integer range 1 .. 100;
type Array_Type is array (Index_Type range <>) of Item_Type;
procedure Search (Source : in Array_Type;
Item : in Item_Type;
Found ... |
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
... | #Picat | Picat | foreach(I in [5,50,900])
println(to_binary_string(I))
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
... | #PicoLisp | PicoLisp | : (bin 5)
-> "101"
: (bin 50)
-> "110010"
: (bin 9000)
-> "10001100101000" |
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... | #Standard_ML | Standard ML | fun binary_search cmp (key, arr) =
let
fun aux slice =
if ArraySlice.isEmpty slice then
NONE
else
let
val mid = ArraySlice.length slice div 2
in
case cmp (ArraySlice.sub (slice, mid), key)
of LESS => aux (ArraySlice.subslice (slice, mid+1, NONE))
| GREATER => ... |
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
... | #Piet | Piet | ? 5
101
? 50
110010
? 9000
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
... | #PL.2FI | PL/I | put edit (25) (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... | #Swift | Swift | func binarySearch<T: Comparable>(xs: [T], x: T) -> Int? {
var recurse: ((Int, Int) -> Int?)!
recurse = {(low, high) in switch (low + high) / 2 {
case _ where high < low: return nil
case let mid where xs[mid] > x: return recurse(low, mid - 1)
case let mid where xs[mid] < x: return recurse(mid + 1, high)
... |
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
... | #PL.2FM | PL/M | 100H:
/* CP/M BDOS CALL */
BDOS: PROCEDURE (FN, ARG);
DECLARE FN BYTE, ARG ADDRESS;
GO TO 5;
END BDOS;
/* PRINT STRING */
PRINT: PROCEDURE (STRING);
DECLARE STRING ADDRESS;
CALL BDOS(9, STRING);
END PRINT;
/* PRINT BINARY NUMBER */
PRINT$BINARY: PROCEDURE (N);
DECLARE S (19) BYTE INITIAL ('...... |
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
... | #PowerBASIC | PowerBASIC |
#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
FUNCTION PBMAIN () AS LONG
LOCAL i, d() AS DWORD
REDIM d(2)
ARRAY ASSIGN d() = 5, 50, 9000
FOR i = 0 TO 2
PRINT STR$(d(i)) & ": " & BIN$(d(i)) & " (" & BIN$(d(i), 32) & ")"
NEXT i
END FUNCTION |
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... | #Symsyn | Symsyn |
a : 1 : 2 : 27 : 44 : 46 : 57 : 77 : 154 : 212
binary_search param item index size
index saveindex
index L
(index + size - 1) H
if L <= H
((L + H) shr 1) M
if base.M > item
- 1 M H
else
if base.M < item
+ 1 M L
else
- saveindex M
return M
... |
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
... | #PowerShell | PowerShell | @(5,50,900) | foreach-object { [Convert]::ToString($_,2) } |
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
... | #Processing | Processing | println(Integer.toBinaryString(5)); // 101
println(Integer.toBinaryString(50)); // 110010
println(Integer.toBinaryString(9000)); // 10001100101000 |
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... | #Tcl | Tcl | proc binSrch {lst x} {
set len [llength $lst]
if {$len == 0} {
return -1
} else {
set pivotIndex [expr {$len / 2}]
set pivotValue [lindex $lst $pivotIndex]
if {$pivotValue == $x} {
return $pivotIndex
} elseif {$pivotValue < $x} {
set recursive ... |
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
... | #Prolog | Prolog |
binary(X) :- format('~2r~n', [X]).
main :- maplist(binary, [5,50,9000]), halt.
|
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... | #TI-83_BASIC | TI-83 BASIC | PROGRAM:BINSEARC
:Disp "INPUT A LIST:"
:Input L1
:SortA(L1)
:Disp "INPUT A NUMBER:"
:Input A
:1→L
:dim(L1)→H
:int(L+(H-L)/2)→M
:While L<H and L1(M)≠A
:If A>M
:Then
:M+1→L
:Else
:M-1→H
:End
:int(L+(H-L)/2)→M
:End
:If L1(M)=A
:Then
:Disp A
:Disp "IS AT POSITION"
:Disp M
:Else
:Disp A
:Disp "IS NOT IN"
:Disp L1 |
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
... | #PureBasic | PureBasic | If OpenConsole()
PrintN(Bin(5)) ;101
PrintN(Bin(50)) ;110010
PrintN(Bin(9000)) ;10001100101000
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf |
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... | #uBasic.2F4tH | uBasic/4tH | For i = 1 To 100 ' Fill array with some values
@(i-1) = i
Next
Print FUNC(_binarySearch(50,0,99)) ' Now find value '50'
End ' and prints its index
_binarySearch Param(3) ' value, start index, end index
Local(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
... | #Python | Python | >>> for i in range(16): print('{0:b}'.format(i))
0
1
10
11
100
101
110
111
1000
1001
1010
1011
1100
1101
1110
1111 |
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... | #UNIX_Shell | UNIX Shell |
#!/bin/ksh
# This should work on any clone of Bourne Shell, ksh is the fastest.
value=$1; [ -z "$value" ] && exit
array=()
size=0
while IFS= read -r line; do
size=$(($size + 1))
array[${#array[*]}]=$line
done
|
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
... | #QB64 | QB64 |
Print DecToBin$(5)
Print DecToBin$(50)
Print DecToBin$(9000)
Print BinToDec$(DecToBin$(5)) ' 101
Print BinToDec$(DecToBin$(50)) '110010
Print BinToDec$(DecToBin$(9000)) ' 10001100101000
End
Function DecToBin$ (digit As Integer)
DecToBin$ = "Error"
If digit < 1 Then
Print " Error number invalid ... |
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
... | #Quackery | Quackery |
2 base put ( Numbers will be output in base 2 now. )
( Bases from 2 to 36 (inclusive) are supported. )
5 echo cr
50 echo cr
9000 echo cr
base release ( It's best to clean up after ourselves. )
( Numbers will be output in base 10 now. )
|
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... | #UnixPipes | UnixPipes | splitter() {
a=$1; s=$2; l=$3; r=$4;
mid=$(expr ${#a[*]} / 2);
echo $s ${a[*]:0:$mid} > $l
echo $(($mid + $s)) ${a[*]:$mid} > $r
}
bsearch() {
(to=$1; read s arr; a=($arr);
test ${#a[*]} -gt 1 && (splitter $a $s >(bsearch $to) >(bsearch $to)) || (test "$a" -eq "$to" && echo $a at $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
... | #R | R |
dec2bin <- function(num) {
ifelse(num == 0,
0,
sub("^0+","",paste(rev(as.integer(intToBits(num))), collapse = ""))
)
}
for (anumber in c(0, 5, 50, 9000)) {
cat(dec2bin(anumber),"\n")
}
|
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... | #VBA | VBA | Public Function BinarySearch(a, value, low, high)
'search for "value" in ordered array a(low..high)
'return index point if found, -1 if not found
If high < low Then
BinarySearch = -1 'not found
Exit Function
End If
midd = low + Int((high - low) / 2) ' "midd" because "Mid" is reserved in VBA
If a(midd) ... |
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
... | #Racket | Racket |
#lang racket
;; Option 1: binary formatter
(for ([i 16]) (printf "~b\n" i))
;; Option 2: explicit conversion
(for ([i 16]) (displayln (number->string i 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... | #VBScript | VBScript | Function binary_search(arr,value,lo,hi)
If hi < lo Then
binary_search = 0
Else
middle=Int((hi+lo)/2)
If value < arr(middle) Then
binary_search = binary_search(arr,value,lo,middle-1)
ElseIf value > arr(middle) Then
binary_search = binary_search(arr,value,middle+1,hi)
Else
binary_search = m... |
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
... | #Raku | Raku | say .fmt("%b") for 5, 50, 9000; |
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... | #Vedit_macro_language | Vedit macro language | // Main program for testing BINARY_SEARCH
#3 = Get_Num("Value to search: ")
EOF
#2 = Cur_Line // hi
#1 = 1 // lo
Call("BINARY_SEARCH")
Message("Value ") Num_Type(#3, NOCR)
if (Return_Value < 1) {
Message(" not found\n")
} else {
Message(" found at index ") Num_Type(Ret... |
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
... | #RapidQ | RapidQ |
'Convert Integer to binary string
Print "bin 5 = ", bin$(5)
Print "bin 50 = ",bin$(50)
Print "bin 9000 = ",bin$(9000)
sleep 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
... | #Red | Red | Red []
foreach number [5 50 9000] [
;; any returns first not false value, used to cut leading zeroes
binstr: form any [find enbase/base to-binary number 2 "1" "0"]
print reduce [ pad/left number 5 binstr ]
]
|
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... | #Visual_Basic_.NET | Visual Basic .NET | Function BinarySearch(ByVal A() As Integer, ByVal value As Integer) As Integer
Dim low As Integer = 0
Dim high As Integer = A.Length - 1
Dim middle As Integer = 0
While low <= high
middle = (low + high) / 2
If A(middle) > value Then
high = middle - 1
ElseIf A(middle... |
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
... | #Retro | Retro | 9000 50 5 3 [ binary putn cr decimal ] times |
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
... | #REXX | REXX | /*REXX program to convert several decimal numbers to binary (or base 2). */
numeric digits 1000 /*ensure we can handle larger numbers. */
@.=; @.1= 0
@.2= 5
@.3= 50
@.4= 9000
do j=1 while @.j\=='' ... |
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... | #Vlang | Vlang | fn binary_search_rec(a []f64, value f64, low int, high int) int { // recursive
if high <= low {
return -1
}
mid := (low + high) / 2
if a[mid] > value {
return binary_search_rec(a, value, low, mid-1)
} else if a[mid] < value {
return binary_search_rec(a, value, mid+1, high)
... |
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
... | #Ring | Ring |
see "Number to convert : "
give a
n = 0
while pow(2,n+1) < a
n = n + 1
end
for i = n to 0 step -1
x = pow(2,i)
if a >= x see 1 a = a - x
else see 0 ok
next
|
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... | #Wortel | Wortel | ; Recursive
@var rec &[a v l h] [
@if < h l @return null
@var m @/ +h l 2
@? {
> `m a v @!rec[a v l -m 1]
< `m a v @!rec[a v +1 m h]
m
}
]
; Iterative
@var itr &[a v] [
@vars{l 0 h #-a}
@while <= l h [
@var m @/ +l h 2
@iff {
> `m a v :h -m 1
< `m a v :l +m 1
@return ... |
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
... | #Ruby | Ruby | [5,50,9000].each do |n|
puts "%b" % n
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
... | #Run_BASIC | Run BASIC | input "Number to convert:";a
while 2^(n+1) < a
n = n + 1
wend
for i = n to 0 step -1
x = 2^i
if a >= x then
print 1;
a = a - x
else
print 0;
end if
next |
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... | #Wren | Wren | class BinarySearch {
static recursive(a, value, low, high) {
if (high < low) return -1
var mid = low + ((high - low)/2).floor
if (a[mid] > value) return recursive(a, value, low, mid-1)
if (a[mid] < value) return recursive(a, value, mid+1, high)
return mid
}
static i... |
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
... | #Rust | Rust | fn main() {
for i in 0..8 {
println!("{:b}", i)
}
} |
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
... | #S-lang | S-lang | define int_to_bin(d)
{
variable m = 0x40000000, prn = 0, bs = "";
do {
if (d & m) {
bs += "1";
prn = 1;
}
else if (prn)
bs += "0";
m = m shr 1;
} while (m);
if (bs == "") bs = "0";
return bs;
}
() = printf("%s\n", int_to_bin(5));
() = printf("%s\n", int_to_bin(50));
() ... |
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... | #XPL0 | XPL0 |
\Binary search
code CrLf=9, IntOut=11, Text=12;
def Size = 10;
integer A, X, I;
function integer DoBinarySearch(A, N, X);
integer A, N, X;
integer L, H, M;
begin
L:= 0; H:= N - 1;
while L <= H do
begin
M:= L + (H - L) / 2;
case of
A(M) < X: L:= M + 1;
A(M) > X: H:= M - 1
oth... |
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
... | #Scala | Scala | scala> (5 toBinaryString)
res0: String = 101
scala> (50 toBinaryString)
res1: String = 110010
scala> (9000 toBinaryString)
res2: String = 10001100101000 |
Subsets and Splits
Rosetta Code COBOL Python Hard Tasks
Identifies and retrieves challenging tasks that exist in both COBOL and Python, revealing cross-language programming patterns and difficulty levels for comparative analysis.
Rosetta Code Task Comparisons
Identifies tasks common to both COBOL and Python languages that are described as having difficulty levels, revealing cross-language task similarities and providing useful comparative programming examples.
Select Specific Languages Codes
Retrieves specific programming language names and codes from training data, providing basic filtering but limited analytical value beyond identifying these particular languages.