Skip to content

Instantly share code, notes, and snippets.

View ducaale's full-sized avatar

Mohamed Daahir ducaale

View GitHub Profile
@ducaale
ducaale / bits.rs
Last active December 29, 2022 18:34
Bit manipulation in Rust
fn main() {
// format a number in two’s complement representation
let a: i8 = -1;
assert_eq!(format!("{:b}", a), "11111111");
// concatenate 3 bit slices
let b: i8 = (0b10 << 4) + (0b10 << 2) + (0b10);
assert_eq!(b, 0b101010);
@ducaale
ducaale / sql-template-string.js
Last active April 26, 2020 20:02
Generating SQL prepared statements with Javascript tagged template literals
const queryObjectId = Symbol("sql_query")
const sql = (string, ...values) => {
const isQuery = query => query.hasOwnProperty(queryObjectId)
let text = ''
for (const [i, s] of string.entries()) {
text += s
if (i !== string.length - 1) {
if (isQuery(values[i])) {