https://thedailywtf.com/articles/a-bit-overcomplicated [wtf-logo] * Feature Articles + Most Recent Articles + Classic WTF: Consultants of the Crystal Citadel + Classic WTF: The Mainframe Database + Classic WTF: Front-Ahead Design + Classic WTF: Gaming the System + Who Tests the Tester? + A Specified Integration + Announcing the launch of TFTs + Reinventing the Wheel * CodeSOD + Most Recent Articles + A Bit Overcomplicated + On Error Goto Anywhere but Here + Changing Your Type + Select Orders + Extensioning Yourself + All the News You Need + Without Any Padding + Ordering the Hash * Error'd + Most Recent Articles + It's Funny Because It's True + By The Clicking On My Thumbs + Innocents Abroad + Malice Reflected + The Past is Prologue + Production Testing To The Max + Some Sunny Day + Unspoken * [forums-ico] [forums-ico] Forums * Other Articles + Random Article + Other Series + Alex's Soapbox + Announcements + Best of... + Best of Email + Best of the Sidebar + Bring Your Own Code + Coded Smorgasbord + Mandatory Fun Day + Off Topic + Representative Line + News Roundup + Editor's Soapbox + Software on the Rocks + Souvenir Potpourri + Sponsor Post + Tales from the Interview + The Daily WTF: Live + Virtudyne [ ] [mobile-men] * feature articles * codesod * error'd * forums * other articles * random article [0586e56c22] A Bit Overcomplicated by Remy Porter in CodeSOD on 2021-08-05 Edit [Remy203-30] Remy Porter Remy escaped the enterprise world and now makes LEDs blink pretty. Editor-in-Chief for TDWTF. Lets say you have a 64-bit integer. You want the first 42-bits. Now, if your language has a bitshift operator, you'd be tempted to do something like largeNumber >> 22. But what if your language also has all sorts of advanced stream processing and map functions? It'd be a shame to not use those. Tarah 's shop uses Rust, and thus they do have all those exciting advanced stream processing. Her co-worker decided they just needed to be used: let binary = format!("{:b}", snowflake); let in_binary: u64 = binary[..42] .to_string() .chars() .rev() .enumerate() .map(|(idx, digit)| { (u64::from_str_radix(&digit.to_string(), 2).unwrap()) * ((2_u64).pow(idx as u32)) }) .sum(); This starts by taking a number, and converting it into a string containing the binary representation. Then we chop off the first 42 characters of that string, break them into characters, reverse the stream, enumerate it, and then apply a map where we parse each character back into a number (which will be 0 or 1), and then multiply it by two raised to the power of the index we're on. sum all that up, and we've got the first 42 bits of a 64-bit number. And sure, snowflake >> 22 would be shorter, clearer, and far more efficient, but it wouldn't let you show off all the cool language features you have at your disposal, and isn't that what's really important? [otter-icon] [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today! View all 25 comments >> * [email-24] * [googleplus] * [facebook-2] * [twitter-24] --------------------------------------------------------------------- << On Error Goto Anywhere but Here Contact Privacy Policy RSS --------------------------------------------------------------------- Copyright (c) 2004 - 2017 Inedo Publishing - v2020.2 Monitored by Panopta * Deployed with BuildMaster