19 votes

Find if there are consecutive 1s in a binary representation of a number

Update: just use BitAnd[x, BitShiftRight[x, 1]] > 0. It's 10x faster than below. Bit-level parallelism beats multiple shifts every time. This method is super ...
flinty's user avatar
  • 25.3k
16 votes
Accepted

Convert four bytes to IEEE 754 float

...
eyorble's user avatar
  • 9,458
14 votes

How to total the amount of zeros in a binary matrix

ClearAll[countZ] countZ = 1 ## & @@ Dimensions @ # - Total[#, 2] &; countZ@binarym 17 Timings: ...
kglr's user avatar
  • 395k
13 votes
Accepted

What is the relationship between the Wolfram executables? Where is this documented?

math/wolfram, MathKernel/WolframKernel are all the same: ...
Szabolcs's user avatar
  • 235k
12 votes
Accepted

Find if there are consecutive 1s in a binary representation of a number

It seems like directly constructing the list might be the fastest method. Most numbers will have consecutive ones. Based on the wrap-around criteria, we already know that testing any odd number is a ...
MassDefect's user avatar
  • 10.1k
12 votes

Using Mathematica to derive analytic form of single variable function

If one makes a table of the first few values of $n$, then a pattern becomes evident: Table[{n, f[n]}, {n, 3, 8}] // TableForm One can see that the leading term is ...
JimB's user avatar
  • 41.7k
11 votes

BitNot does not flip bits in the way I expected

twosComplement[x_, n_] := UnitBox@IntegerDigits[x, 2, n] twosComplement[35, 8] {1, 1, 0, 1, 1, 1, 0, 1}
OkkesDulgerci's user avatar
11 votes
Accepted

Generating a matrix with specific number of 1's in every row and column

...
kglr's user avatar
  • 395k
10 votes
Accepted

Substitute some 1s with 0s in random list

Updated to include suggestions from comments My original idea, using BitAnd, switching $0\leftrightarrow1$, and multiplying. This idea uses 3 vectorized binary ...
Carl Woll's user avatar
  • 131k
10 votes

Turn binary number into string of words

...
Bob Hanlon's user avatar
  • 158k
10 votes
Accepted

BitNot does not flip bits in the way I expected

I don't think there is a built-in function to generate the two's complement representation. Easy to implement though. ...
Rohit Namjoshi's user avatar
10 votes
Accepted

Something is wrong with the implementation of the ThueMorse function

The problem is the following: Mod[Plus @@ IntegerDigits[n, 2], 2] Mod[2 + n, 2] This happens because ...
Greg Hurst's user avatar
10 votes

Find if there are consecutive 1s in a binary representation of a number

For the sake of getting answers for higher values of dim, I present to you some bit manipulation hacking for dim=20: ...
eyorble's user avatar
  • 9,458
9 votes

How to find the sum for each individual row in a binary matrix until the first zero is reached from left to right.

LengthWhile[#, Positive] & /@ binarym {3, 4, 2, 4, 6} Or binarym /. {x : Longest[1 ..], __} :> Length[{x}]
eldo's user avatar
  • 70.4k
9 votes
Accepted

Is it possible to convert the output of the Encrypt[] function into binary digits?

I will turn Kuba's comment into an answer. The EncryptedObject you create using Encrypt contains the encrypted data and the ...
M. Stern's user avatar
  • 1,159
9 votes

Generating a matrix with specific number of 1's in every row and column

My package IGraph/M has several functions for this. But before you get started, think carefully about what you mean by "random". I am going to assume that you want to generate each such ...
Szabolcs's user avatar
  • 235k
9 votes

Convert four bytes to IEEE 754 float

As @DanielLichtblau points out, ImportByteArray is surprisingly slow. Here's a LibraryLink code that uses a bare-metal byte-to-float type cast. Its advantages are: ...
Roman's user avatar
  • 47.5k
8 votes
Accepted

How is the mysterious Raw function used?

The short answer is, you can't. The only function that can really consume these objects is not exposed at the language level but only in the kernel's own C code. To be honest, am not sure why it is ...
Itai Seggev's user avatar
  • 14.1k
8 votes
Accepted

Reading a file as a ByteArray?

Import[..., "String"] and Export[..., "String"] are meant precisely for this and will not cause problems. This is guaranteed to ...
Szabolcs's user avatar
  • 235k
8 votes

Substitute some 1s with 0s in random list

The following function is based on the Ramp and Differences functions, as suggested in a comment by @garej . Its speed and low ...
LouisB's user avatar
  • 12.5k
8 votes

How to find the sum for each individual row in a binary matrix until the first zero is reached from left to right.

Update: ff works as is in version 9. For versions 10+, as commented by @Mr.Wizard, it needs to be modified to prevent the index i...
kglr's user avatar
  • 395k
8 votes

How to get the bit size (bit length) of an integer?

You can use BitLength or IntegerLength: BitLength[number] 4091 ...
kglr's user avatar
  • 395k
8 votes

Row function to join together binary number elements

Let's define a function that joins together two of the entries: join[{x_, y_}] := ToExpression[StringJoin[{ToString[x], ToString[y]}]]; Now we can use this ...
bill s's user avatar
  • 69k
8 votes

Row function to join together binary number elements

Map[FromDigits @* Flatten @* IntegerDigits, testlist, {2}] ...
kglr's user avatar
  • 395k
7 votes

How to find the sum for each individual row in a binary matrix until the first zero is reached from left to right.

First /@ SparseArray[1 - binarym]["MatrixColumns"] - 1 {3, 4, 2, 4, 6}
yode's user avatar
  • 26.7k
7 votes
Accepted

Printing binary numbers with extra zeros

It's unclear in your question if you are asking for a String for NumberForm. FORM If you want to change the Form but not ...
rhermans's user avatar
  • 36.5k
7 votes

BitNot does not flip bits in the way I expected

Without using IntegerDigits[]: ...
J. M.'s missing motivation's user avatar
7 votes

Convert four bytes to IEEE 754 float

As shown already, this can be done by ImportByteArray. If you have long lists, this can be slow. Here is a test. ...
Daniel Lichtblau's user avatar
7 votes
Accepted

DigitCount[], but in negative bases

Background mathworld.wolfram.com contains entries for Negabinary, Negadecimal and Base, including Wolfram language code examples Base $(-2)$ Negabinary ...
rhermans's user avatar
  • 36.5k
6 votes
Accepted

Performance-driven approach to using Big Data without killing the hard-drive

So, a large part of this question seems to be concerned with whether or not BinaryReadList can cause meaningful damage to an underlying drive (SSD or HDD). From ...
eyorble's user avatar
  • 9,458

Only top scored, non community-wiki answers of a minimum length are eligible