<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: binary code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 06:21:14 GMT</pubDate>
    <description>DZone Snippets: binary code</description>
    <item>
      <title>Format IPv4 address in octal binary format and vice versa (Ruby / Rails)</title>
      <link>http://snippets.dzone.com/posts/show/4931</link>
      <description>Format an IPv4 address like 192.168.1.1 in dotted binary format like 11000000.10101000.00000001.00000001&lt;br /&gt;You also need this class: http://snippets.dzone.com/posts/show/2472&lt;br /&gt;For Rails: Put this in your controller!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # convert a dotted decimal IPv4 address to dotted binary format&lt;br /&gt;  def ipv4_to_binary(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_i.to_s(2).using("########","0",true)&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  # convert a IPv4 adress in binary dotted format to a dotted IPv4 address&lt;br /&gt;  def binary_to_ipv4(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_s.to_i(2).to_s&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Dec 2007 14:12:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4931</guid>
      <author>MichaelWhi (Michael Whittaker)</author>
    </item>
    <item>
      <title>Bitwise Operation: Quickly Convert Binary Bit to Integer and vice versa using Javascript Shell</title>
      <link>http://snippets.dzone.com/posts/show/4740</link>
      <description>using JavaScript shell found at www.squarefree.com/shell/ , it's very easy to convert binary bit to integer and vice versa&lt;br /&gt;&lt;br /&gt;convert integer to binary bit string&lt;br /&gt;&lt;code&gt;&lt;br /&gt;num = 1024&lt;br /&gt;num.toString(2)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;convert binary bit string to integer&lt;br /&gt;&lt;code&gt;&lt;br /&gt;parseInt('111', 2)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 07 Nov 2007 05:56:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4740</guid>
      <author>agilekai (kai)</author>
    </item>
    <item>
      <title>Print a binary number in C</title>
      <link>http://snippets.dzone.com/posts/show/4716</link>
      <description>These are two functions that print the binary representation of an integer. The first simply prints it out, while the second only prints out the relevant digits (i.e. cuts the first x 0 digits) in groups of four.&lt;br /&gt;&lt;br /&gt;Explanation &lt;a href="http://compprog.wordpress.com/2007/10/29/bit-operations/"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* Print n as a binary number */&lt;br /&gt;void printbitssimple(int n) {&lt;br /&gt;	unsigned int i;&lt;br /&gt;	i = 1&lt;&lt;(sizeof(n) * 8 - 1);&lt;br /&gt;&lt;br /&gt;	while (i &gt; 0) {&lt;br /&gt;		if (n &amp; i)&lt;br /&gt;			printf("1");&lt;br /&gt;		else&lt;br /&gt;			printf("0");&lt;br /&gt;		i &gt;&gt;= 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Print n as a binary number */&lt;br /&gt;void printbits(int n) {&lt;br /&gt;	unsigned int i, step;&lt;br /&gt;&lt;br /&gt;	if (0 == n) { /* For simplicity's sake, I treat 0 as a special case*/&lt;br /&gt;		printf("0000");&lt;br /&gt;		return;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	i = 1&lt;&lt;(sizeof(n) * 8 - 1);&lt;br /&gt;&lt;br /&gt;	step = -1; /* Only print the relevant digits */&lt;br /&gt;	step &gt;&gt;= 4; /* In groups of 4 */&lt;br /&gt;	while (step &gt;= n) {&lt;br /&gt;		i &gt;&gt;= 4;&lt;br /&gt;		step &gt;&gt;= 4;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	/* At this point, i is the smallest power of two larger or equal to n */&lt;br /&gt;	while (i &gt; 0) {&lt;br /&gt;		if (n &amp; i)&lt;br /&gt;			printf("1");&lt;br /&gt;		else&lt;br /&gt;			printf("0");&lt;br /&gt;		i &gt;&gt;= 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; i &lt; 16; ++i) {&lt;br /&gt;		printf("%d = ", i);&lt;br /&gt;		printbitssimple(i);&lt;br /&gt;		printf("\n");&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 30 Oct 2007 11:53:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4716</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Generate all subsets of a set</title>
      <link>http://snippets.dzone.com/posts/show/4631</link>
      <description>This code generates all the subsets of {1, 2, ..., n} and prints them.&lt;br /&gt;&lt;br /&gt;This also contains a very fast binary counter implementation.&lt;br /&gt;&lt;br /&gt;See &lt;a href="http://compprog.wordpress.com/2007/10/10/generating-subsets/"&gt;this&lt;/a&gt; for further explanations.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* Applies the mask to a set like {1, 2, ..., n} and prints it */&lt;br /&gt;void printv(int mask[], int n) {&lt;br /&gt;	int i;&lt;br /&gt;	printf("{ ");&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		if (mask[i])&lt;br /&gt;			printf("%d ", i + 1); /*i+1 is part of the subset*/&lt;br /&gt;	printf("\b }\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Generates the next mask*/&lt;br /&gt;int next(int mask[], int n) {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; (i &lt; n) &amp;&amp; mask[i]; ++i)&lt;br /&gt;		mask[i] = 0;&lt;br /&gt;&lt;br /&gt;	if (i &lt; n) {&lt;br /&gt;		mask[i] = 1;&lt;br /&gt;		return 1;&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int n = 3;&lt;br /&gt;&lt;br /&gt;	int mask[16]; /* Guess what this is */&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		mask[i] = 0;&lt;br /&gt;&lt;br /&gt;	/* Print the first set */&lt;br /&gt;	printv(mask, n);&lt;br /&gt;&lt;br /&gt;	/* Print all the others */&lt;br /&gt;	while (next(mask, n))&lt;br /&gt;		printv(mask, n);&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 Oct 2007 14:43:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4631</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Binary-to-text converter</title>
      <link>http://snippets.dzone.com/posts/show/4248</link>
      <description>Converts all 8-bit binary numbers in the standard input into text, though there's probably a better way to do this&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl -wn&lt;br /&gt;use strict;&lt;br /&gt;s/\s//g;&lt;br /&gt;print chr oct "0b$1" while /([01]{8})/g;&lt;br /&gt;print "\n";&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 02:37:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4248</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>BitField: A fast(ish), pure Ruby bit field "type"</title>
      <link>http://snippets.dzone.com/posts/show/4234</link>
      <description>&lt;code&gt;#        NAME: BitField&lt;br /&gt;#      AUTHOR: Peter Cooper&lt;br /&gt;#     LICENSE: MIT ( http://www.opensource.org/licenses/mit-license.php )&lt;br /&gt;#   COPYRIGHT: (c) 2007 Peter Cooper (http://www.petercooper.co.uk/)&lt;br /&gt;#     VERSION: v4&lt;br /&gt;#     HISTORY: v4 (fixed bug where setting 0 bits to 0 caused a set to 1)&lt;br /&gt;#              v3 (supports dynamic bitwidths for array elements.. now doing 32 bit widths default)&lt;br /&gt;#              v2 (now uses 1 &lt;&lt; y, rather than 2 ** y .. it's 21.8 times faster!)&lt;br /&gt;#              v1 (first release)&lt;br /&gt;#&lt;br /&gt;# DESCRIPTION: Basic, pure Ruby bit field. Pretty fast (for what it is) and memory efficient.&lt;br /&gt;#              I've written a pretty intensive test suite for it and it passes great. &lt;br /&gt;#              Works well for Bloom filters (the reason I wrote it).&lt;br /&gt;#&lt;br /&gt;#              Create a bit field 1000 bits wide&lt;br /&gt;#                bf = BitField.new(1000)&lt;br /&gt;#&lt;br /&gt;#              Setting and reading bits&lt;br /&gt;#                bf[100] = 1&lt;br /&gt;#                bf[100]    .. =&gt; 1&lt;br /&gt;#                bf[100] = 0&lt;br /&gt;#&lt;br /&gt;#              More&lt;br /&gt;#                bf.to_s = "10101000101010101"  (example)&lt;br /&gt;#                bf.total_set         .. =&gt; 10  (example - 10 bits are set to "1")&lt;br /&gt;&lt;br /&gt;class BitField&lt;br /&gt;  attr_reader :size&lt;br /&gt;  include Enumerable&lt;br /&gt;  &lt;br /&gt;  ELEMENT_WIDTH = 32&lt;br /&gt;  &lt;br /&gt;  def initialize(size)&lt;br /&gt;    @size = size&lt;br /&gt;    @field = Array.new(((size - 1) / ELEMENT_WIDTH) + 1, 0)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Set a bit (1/0)&lt;br /&gt;  def []=(position, value)&lt;br /&gt;    if value == 1&lt;br /&gt;      @field[position / ELEMENT_WIDTH] |= 1 &lt;&lt; (position % ELEMENT_WIDTH)&lt;br /&gt;    elsif (@field[position / ELEMENT_WIDTH]) &amp; (1 &lt;&lt; (position % ELEMENT_WIDTH)) != 0&lt;br /&gt;      @field[position / ELEMENT_WIDTH] ^= 1 &lt;&lt; (position % ELEMENT_WIDTH)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Read a bit (1/0)&lt;br /&gt;  def [](position)&lt;br /&gt;    @field[position / ELEMENT_WIDTH] &amp; 1 &lt;&lt; (position % ELEMENT_WIDTH) &gt; 0 ? 1 : 0&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Iterate over each bit&lt;br /&gt;  def each(&amp;block)&lt;br /&gt;    @size.times { |position| yield self[position] }&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Returns the field as a string like "0101010100111100," etc.&lt;br /&gt;  def to_s&lt;br /&gt;    inject("") { |a, b| a + b.to_s }&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # Returns the total number of bits that are set&lt;br /&gt;  # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)&lt;br /&gt;  def total_set&lt;br /&gt;    @field.inject(0) { |a, byte| a += byte &amp; 1 and byte &gt;&gt;= 1 until byte == 0; a }&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here's the tests if you want to run:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "test/unit"&lt;br /&gt;require "bitfield"&lt;br /&gt;&lt;br /&gt;class TestLibraryFileName &lt; Test::Unit::TestCase&lt;br /&gt;  def setup&lt;br /&gt;    @public_bf = BitField.new(1000)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_basic&lt;br /&gt;    assert_equal 0, BitField.new(100)[0]&lt;br /&gt;    assert_equal 0, BitField.new(100)[1]&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_setting_and_unsetting&lt;br /&gt;    @public_bf[100] = 1&lt;br /&gt;    assert_equal 1, @public_bf[100]&lt;br /&gt;    @public_bf[100] = 0&lt;br /&gt;    assert_equal 0, @public_bf[100]&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def test_random_setting_and_unsetting&lt;br /&gt;    100.times do&lt;br /&gt;      index = rand(1000)&lt;br /&gt;      @public_bf[index] = 1&lt;br /&gt;      assert_equal 1, @public_bf[index]&lt;br /&gt;      @public_bf[index] = 0&lt;br /&gt;      assert_equal 0, @public_bf[index]&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_multiple_setting&lt;br /&gt;    1.upto(999) do |pos|&lt;br /&gt;      2.times { @public_bf[pos] = 1 }&lt;br /&gt;      assert_equal 1, @public_bf[pos]&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def test_multiple_unsetting&lt;br /&gt;    1.upto(999) do |pos|&lt;br /&gt;      2.times { @public_bf[pos] = 0 }&lt;br /&gt;      assert_equal 0, @public_bf[pos]&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_size&lt;br /&gt;    assert_equal 1000, @public_bf.size&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_to_s&lt;br /&gt;    bf = BitField.new(10)&lt;br /&gt;    bf[1] = 1&lt;br /&gt;    bf[5] = 1&lt;br /&gt;    assert_equal "0100010000", bf.to_s&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_total_set&lt;br /&gt;    bf = BitField.new(10)&lt;br /&gt;    bf[1] = 1&lt;br /&gt;    bf[5] = 1&lt;br /&gt;    assert_equal 2, bf.total_set&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Jul 2007 01:16:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4234</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Count number of set bits in an integer</title>
      <link>http://snippets.dzone.com/posts/show/4233</link>
      <description>&lt;code&gt;&lt;br /&gt;count = 0&lt;br /&gt;count += byte &amp; 1 and byte &gt;&gt;= 1 until byte == 0&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Jul 2007 01:12:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4233</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>binarysearch.scm</title>
      <link>http://snippets.dzone.com/posts/show/3535</link>
      <description>// Binary Search&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;; Andrew Pennebaker&lt;br /&gt;; 18 Feb 2007&lt;br /&gt;; License: GPL&lt;br /&gt;; URL: http://snippets.dzone.com/posts/show/3535&lt;br /&gt;&lt;br /&gt;(define binary-search&lt;br /&gt;	(lambda (ls value low high)&lt;br /&gt;		(let ((mid (floor (/ (+ low high) 2))))&lt;br /&gt;			(cond&lt;br /&gt;				((&gt; low high) -1)&lt;br /&gt;				((= (list-ref ls mid) value) mid)&lt;br /&gt;				((&gt; (list-ref ls mid) value) (binary-search ls value low (- mid 1)))&lt;br /&gt;				((&lt; (list-ref ls mid) value) (binary-search ls value (+ mid 1) high))))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 Feb 2007 00:38:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3535</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
    <item>
      <title>bindec.scm</title>
      <link>http://snippets.dzone.com/posts/show/3479</link>
      <description>// Convert list of 1s and 0s back to base ten number.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;; Andrew Pennebaker&lt;br /&gt;; 5 Feb 2007&lt;br /&gt;; License: GPL&lt;br /&gt;; URL: http://snippets.dzone.com/posts/show/3479&lt;br /&gt;&lt;br /&gt;(define bin-&gt;dec&lt;br /&gt;	(lambda (b)&lt;br /&gt;		(cond&lt;br /&gt;			((integer? b) b)&lt;br /&gt;			((= (length b) 0) 0)&lt;br /&gt;			(else&lt;br /&gt;				(+&lt;br /&gt;					(* (expt 2 (- (length b) 1)) (car b))&lt;br /&gt;					(bin-&gt;dec (cdr b)))))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Feb 2007 03:21:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3479</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
    <item>
      <title>decbin.scm</title>
      <link>http://snippets.dzone.com/posts/show/3478</link>
      <description>// Converts a base ten integer to a list of 1s and 0s&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;; Andrew Pennebaker&lt;br /&gt;; 3 Feb 2007&lt;br /&gt;; License: GPL&lt;br /&gt;; URL: http://snippets.dzone.com/posts/show/3478&lt;br /&gt;&lt;br /&gt;(define dec-&gt;bin&lt;br /&gt;	(lambda (d)&lt;br /&gt;		(cond&lt;br /&gt;			((&lt; d 1) (list 0))&lt;br /&gt;			((= d 1) (list 1))&lt;br /&gt;			((&gt; d 1) (append&lt;br /&gt;				(dec-&gt;bin (floor (/ d 2)))&lt;br /&gt;				(list (if (= (modulo d 2) 0) 0 1)))))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Feb 2007 03:18:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3478</guid>
      <author>mcandre (Andrew Pennebaker)</author>
    </item>
  </channel>
</rss>
