This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- Clojure
- C++
- C++
- C#
- C#
- D
- Dart
- Elixir
- Elixir
- Erlang
- Fortran
- Fortran
- Go
- Go
- Haskell
- Haskell
- JS
- Java
- Lisp
- Lua
- PHP
- Pascal
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Scala
- Scheme
(defn s
[x]
(pprint/cl-format nil "~b" x))
Call s and pass an integer x to it and it will return your integer in base 2.
std::string ToBinary(int x) {
std::array<char, 64> buf;
auto[ptr, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), x, 2);
auto s = std::string(buf.data(), ptr);
return s;
}
The 2 at the end of to_chars is for base 2
foldl (\acc -> (++ acc) . show) "" . unfoldr (\n -> if x == 0 then Nothing else Just (x `mod` 2, x `div` 2))
Sure, it will produce the empty string for 0 but it's a catamorphism composed with an anamorphism... I mean how much more Haskell-y do you get? There's even some currying and partial application in there!
(defparameter x 13)
(defvar s)
(setf s (format nil "~B" x))
X and S are defined globally, contrary to the Lisp convention that globals wear earmuffs.
local s = {}
while x > 0 do
local tmp = math.fmod(x,2)
s[#s+1] = tmp
x=(x-tmp)/2
end
s=table.concat(s)