Simulate an edge-triggered D Flip-Flop.

What is D Flip-Flop?

A D flip-flop is an electronic digital device that outputs an inputted data (abbr. D) with synchronization to a clock (abbr. CLK). Usually CLK is a uniform pulse, but in this challenge, CLK may be not uniform.


A bitstring with length \$n\$ will be given as CLK and another bitstring with length \$n-1\$ will be given as D. A bit represents the state of an input during a unit time (tick). CLK leads D by half a tick.


The output (abbr. Q) is a bitstring with same length and starting timepoint as D. If CLK doesn't start with 01, the starting bit of Q is implementation-defined. Q is updated upon a rising moment of CLK to the bit of D at the time. On the other times, Q retains its state.


The red line indicates a rising moment of CLK.

  • Example 1:

D flip-flop

  • Example 2:

D flip-flop 2


  • If CLK isn't a tick longer than D, the entire challenge falls into don't care situation.
  • Though defined as bitstrings, the actual elements of CLK, D, and Q doesn't matter. In this case, if a string is not binary (that is, contains a third character), the entire challenge falls into don't care situation.
  • 5
    \$\begingroup\$ I haven't heard of most of these terms so I don't know what the challenge is asking for without following the link to the Wikipedia page. Please add the the relevant information to the challenge body itself. \$\endgroup\$ – xnor Mar 23 at 4:07
  • \$\begingroup\$ @xnor the Output section actually specifies the output here \$\endgroup\$ – my pronoun is monicareinstate Mar 23 at 4:59
  • 5
    \$\begingroup\$ With the well-illustrated examples, I think I now understand what the output section is asking for. Thinking of the bit strings as time series, whenever the CLK bit changes from 0 to 1, the output bit Q is set to the current value of the bit D. In all other cases, Q stays unchanged from what it was last. \$\endgroup\$ – xnor Mar 23 at 5:51
  • 1
    \$\begingroup\$ The starting bit of Q is implementation-defined. Only if CLK does not start with 01? Or does the starting bit never matter? \$\endgroup\$ – Jitse Mar 23 at 8:29
  • 1
    \$\begingroup\$ @GalenIvanov Yes. \$\endgroup\$ – Dannyu NDos Mar 23 at 19:56

05AB1E, 15 14 13 bytes


First input is \$D\$, second input is \$CLK\$.
Output \$Q\$ as a list of 0s and 1s.

Try it online or verify both test cases (and pretty-print).


ε            # Map over the (implicit) input-list D:
  N          #  Push the current map-index
   TS        #  Push 10 as digit-list: [1,0]
     +       #  Add them to the index: [index+1, index]
 I    è      #  Index both of them into the second input CLK
       Æi    #  If they are [1,0]:
         ©   #   Store the current map-value in variable `®` (without popping)
        ë    #  Else:
         ®   #   Push variable `®`
          Θ  #   And check whether it is exactly 1 (1 if truthy, 0 if falsey)
             #   (which is necessary, because `®` is -1 by default)
             # (after which the list is output implicitly as result)
|improve this answer|||||

J, 27 23 bytes

-4 bytes thanks to Bubbler!


Try it online!

CLK is the left argument; D is the right one. When CLK doesn't start with 01, the starting bits are the same as the first bit of D.

                       ;.1  - cut 
                          ] - the right argument (D)
   (          )             - where
         2 /\               - the previous element 
             [              - of the left argument (CLK)
          <                 - is smaller than the next one 
       }.                   - drop
      1                     - the first item
    1,                      - prepend with 1 (for cutting)
               (     )      - for each cut group
                   $        - make a list   
                #           - of the same length as the group
                    {.      - with elements same as the first one  
                 <@         - and box the list
  ;                         - unbox the sublists and flatten them
[:                          - function composition

K (oK), 27 23bytes

-4 bytes thanks to ngn!


Try it online!

|improve this answer|||||
  • 1
    \$\begingroup\$ J, 23 bytes. \$\endgroup\$ – Bubbler Mar 23 at 23:39
  • \$\begingroup\$ @Bubbler Great, thanks! \$\endgroup\$ – Galen Ivanov Mar 24 at 4:47
  • \$\begingroup\$ {(#x)#*x}' -> &\'|\' \$\endgroup\$ – ngn Mar 25 at 19:14
  • \$\begingroup\$ @ngn Thank you, that's very interesting! \$\endgroup\$ – Galen Ivanov Mar 25 at 19:24

APL (Dyalog Extended), 16 bytes


Try it online!

A dyadic tacit function whose right arg is CLK and left arg is D. Accepts both as Boolean vectors. The starting bit of Q follows that of D.

How it works

∊⊣⊣\¨⍤⊂⍨1,1↓2</⊢  ⍝ Input: left←D, right←CLK
            2</⊢  ⍝ Detect rising edges of CLK
        1,1↓      ⍝ Treat the very first tick as a rising edge
 ⊣    ⊂⍨          ⍝ Partition D into regions where
                  ⍝   each rising edge marks the start of each region
  ⊣\¨⍤            ⍝ On each region, overwrite all elements with the first one
∊                 ⍝ Enlist; form a simple vector
|improve this answer|||||

JavaScript (ES6),  40  39 bytes

Takes input as (clock)(data), where clock and data are arrays of binary digits.


Try it online!

|improve this answer|||||

Io, 56 bytes

The space before := is mandatory, otherwise the colon would be part of the undefined identifier.

Argh. Just 1 byte away from Java!

method(c,d,q :=0;d map(i,x,q=if(c at(i)<c at(i+1),x,q)))

Try it online!


method(c, d,                // Take 2 operands, c and d
    q := 0                  // Set the flip flop to 0
    d map (i, x,            // Map every item in d
                            // With the index i and the
                            // current item x
        q = \               // Set the current flip-flop as:
            if(c at(i)      // If the clock at the same index
            < c at(i + 1),  // Is less than the clock at the next index?
                            // I.e. the clock goes from 0 to 1
                x,          // Set the flip-flop as the current item x
                q           // Otherwise, make an identity function.
            )               // The assigned operand result is automatically
    )                       // returned to the map loop, so no
)                           // explicit reference of the flip-flop in the map.
|improve this answer|||||

Python 3.8, 57 bytes

lambda C,D,x=0:[x:=[x,d][a<b]for d,a,b in zip(D,C,C[1:])]

Try it online!

Input: 2 lists of 0s and 1s, the first list C represents the clock line, and the second list D represents the data line.
Output: The list Q representing the state of the flip-flop through time.


  • for d,a,b in zip(D,C,C[1:]) iterates over the lines, where d is the current data bit, a is the previous clock bit, and b is the current clock bit
  • x stores the previous state of the flip-flop
  • x:=[x,d][a<b] updates the state of the flip-flop: if a<b is 1 (edge-rise moment), then the state is updated to the current data bit d. Otherwise, the state remains the same as previous state.
|improve this answer|||||
  • \$\begingroup\$ Why is there "(pre-release)" behind "Python 3.8"? Python 3.8 was released months ago. \$\endgroup\$ – Mast Mar 23 at 15:05
  • 1
    \$\begingroup\$ @Mast true, I just copied the title from TIO and didn't think much about it \$\endgroup\$ – Surculose Sputum Mar 23 at 15:12

Java 8, 55 bytes

C->D->{int i=0,p=0;for(int c:D)D[i]=C[i]<C[++i]?p=c:p;}

Port of @Arnauld's JavaScript answer, so make sure to upvote him!!

Input as two integer-arrays. Stores the result (\$Q\$) in input \$D\$ instead of returning a new list to save bytes.

Try it online.


C->D->{                  // Method with two integer-arrays as parameter and no return-type
  int i=0,               //  Index-integer, starting at 0
      p=0;               //  Previous integer, starting at 0
  for(int c:D)           //  Loop over the digits of input D:
    D[i]=                //   Set the `i`'th value of array D to:
         C[i]            //    If the `i`'th value of array C
             <C[++i]?    //    is smaller than the `i+1`'th value of array C
                         //    (thus C[i] == 0 and C[i+1] == 1)
                     p=c //     Use the current digit `c`,
                         //     and also replace `p` with this digit
                    :    //    Else (they're [0,0], [1,0] or [1,1]):
                     p;} //     Use digit `p` instead
|improve this answer|||||

Charcoal, 22 bytes


Try it online! Link is to verbose version of code. Explanation:


Initialise Q.


Loop over the elements of D.


Did CLK transition from 0 to 1?


If so then set Q to D.


Output Q.

|improve this answer|||||

C (gcc), 80 67 bytes


Try it online!

Inputs the clock and data as int arrays along with the length of the data array.
Returns the output in the clock array starting at one past the beginning.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.