Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Summary of list of bits responses

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28739] Summary of list of bits responses
  • From: Raul Martinez <rmartinez at vrinc.com>
  • Date: Fri, 11 May 2001 03:38:54 -0400 (EDT)
  • Organization: Vista Research Inc
  • Sender: owner-wri-mathgroup at wolfram.com

--------------D33AE880BD70BF074A1A13D4

To the Mathgroup,


Murray Eisenberg posted a question about converting a list of bits into
a string representing a binary number. Many solutions were offered (none
by me), and I found the diversity so interesting that I've taken the
liberty to compile a summary of the solutions with examples and some
comments. Several of the proposed solutions were identical, and when
duplicates were accounted for six solutions remained. The summary is
contained in the attached Mathematica notebook.

HTH,

Raul Martinez

Staff Scientist
Vista Research, Inc.
Sunnyvale, CA 94085

--------------D33AE880BD70BF074A1A13D4
 filename="listtobits.nb"

(************** Content-type: application/mathematica **************

                    Mathematica-Compatible Notebook

This notebook can be used with any Mathematica-compatible
application, such as Mathematica, MathReader or Publicon. The data
for the notebook starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do
one of the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the
  application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing
the word CacheID, otherwise Mathematica-compatible applications may
try to use invalid cache data.

For more information on notebooks and Mathematica-compatible 
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info at wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from 
Wolfram Research.
*******************************************************************)

(*CacheID: 232*)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[      4164,        156]*)
(*NotebookOutlinePosition[      4795,        178]*)
(*  CellTagsIndexPosition[      4751,        174]*)
(*WindowFrame->Normal*)



Notebook[{
Cell[TextData[StyleBox["DATA",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(listofbits = {0, 1, 1, 0, 1}\)], "Input"],

Cell[BoxData[
    \({0, 1, 1, 0, 1}\)], "Output"]
}, Open  ]],

Cell["\<\
\tThe problem is to convert the above list of bits into a string \
corresponding to the appropriate binary number. \
\>", "Text"],

Cell[TextData[StyleBox["Solution 1",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(ToString[Flatten[listofbits]]\)], "Input"],

Cell[BoxData[
    \("{0, 1, 1, 0, 1}"\)], "Output"]
}, Open  ]],

Cell[TextData[StyleBox["Solution 2",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(StringJoin @@ \(ToString /@ listofbits\)\)], "Input"],

Cell[BoxData[
    \("01101"\)], "Output"]
}, Open  ]],

Cell[TextData[StyleBox["Solution 3",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[{
    \(Map[Apply[StringJoin, Map[ToString, #]] &, 
      listofbits]\), "\[IndentingNewLine]", 
    \(Map[Apply[StringJoin, Map[ToString, #]] &, {listofbits}]\)}], "Input"],

Cell[BoxData[
    \({0, 1, 1, 0, 1}\)], "Output"],

Cell[BoxData[
    \({"01101"}\)], "Output"]
}, Open  ]],

Cell[TextData[StyleBox["Solution 4",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(StringJoin[ToString /@ listofbits]\)], "Input"],

Cell[BoxData[
    \("01101"\)], "Output"]
}, Open  ]],

Cell[TextData[StyleBox["Solution 5",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(ToString[FromDigits[listofbits]]\)], "Input"],

Cell[BoxData[
    \("1101"\)], "Output"]
}, Open  ]],

Cell[TextData[StyleBox["Solution 6",
  FontWeight->"Bold"]], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(Apply[StringJoin, ToString /@ listofbits]\)], "Input"],

Cell[BoxData[
    \("01101"\)], "Output"]
}, Open  ]],

Cell[TextData[{
  StyleBox["REMARKS",
    FontWeight->"Bold"],
  "\n\t1. Solution 1 does not work.\n\t\n\t2. Solutions 2, 4, and 6 give the \
same correct answer, with the appropriate zero padding on the left.\n\t\n\t3. \
Solution 3 works only if the list of bits is contained in a list. The \
resulting correct string is also enclosed in a list.\n\t\n\t4. Solution 5 is \
correct but not zero-padded on the left.\n\t\n\t5. Solutions 2 and 4 seem to \
be identical except for the syntax. IMHO, these two are to be the most \
elegant solutions in the sense that they are the shortest."
}], "Text"]
},
FrontEndVersion->"4.1 for X",
ScreenRectangle->{{0, 1280}, {0, 1024}},
WindowSize->{520, 600},
WindowMargins->{{150, Automatic}, {Automatic, 52}}
]

(*******************************************************************
Cached data follows.  If you edit this Notebook file directly, not
using Mathematica, you must remove the line containing CacheID at
the top of  the file.  The cache data will then be recreated when
you save this file from within Mathematica.
*******************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{
Cell[1705, 50, 62, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[1792, 55, 61, 1, 27, "Input"],
Cell[1856, 58, 49, 1, 27, "Output"]
}, Open  ]],
Cell[1920, 62, 139, 3, 50, "Text"],
Cell[2062, 67, 68, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[2155, 72, 62, 1, 27, "Input"],
Cell[2220, 75, 51, 1, 27, "Output"]
}, Open  ]],
Cell[2286, 79, 68, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[2379, 84, 73, 1, 27, "Input"],
Cell[2455, 87, 41, 1, 27, "Output"]
}, Open  ]],
Cell[2511, 91, 68, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[2604, 96, 186, 3, 43, "Input"],
Cell[2793, 101, 49, 1, 27, "Output"],
Cell[2845, 104, 43, 1, 27, "Output"]
}, Open  ]],
Cell[2903, 108, 68, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[2996, 113, 67, 1, 27, "Input"],
Cell[3066, 116, 41, 1, 27, "Output"]
}, Open  ]],
Cell[3122, 120, 68, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[3215, 125, 65, 1, 27, "Input"],
Cell[3283, 128, 40, 1, 27, "Output"]
}, Open  ]],
Cell[3338, 132, 68, 1, 32, "Text"],

Cell[CellGroupData[{
Cell[3431, 137, 74, 1, 27, "Input"],
Cell[3508, 140, 41, 1, 27, "Output"]
}, Open  ]],
Cell[3564, 144, 596, 10, 248, "Text"]
}
]
*)



(*******************************************************************
End of Mathematica Notebook file.
*******************************************************************)


--------------D33AE880BD70BF074A1A13D4--


  • Prev by Date: Re: Plotting
  • Next by Date: Bug in Integrate?
  • Previous by thread: Help optimizing a slow integral
  • Next by thread: Bug in Integrate?