# Objective

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.

# Input

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.

# Output

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.

# Examples

The red line indicates a rising moment of CLK.

• Example 1:

• Example 2:

# Rules

• 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.
• 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. – xnor Mar 23 at 4:07
• @xnor the Output section actually specifies the output here – my pronoun is monicareinstate Mar 23 at 4:59
• 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. – xnor Mar 23 at 5:51
• The starting bit of Q is implementation-defined. Only if CLK does not start with 01? Or does the starting bit never matter? – Jitse Mar 23 at 8:29
• @GalenIvanov Yes. – Dannyu NDos Mar 23 at 19:56

# 05AB1E, 1514 13 bytes

εINTS+èÆi©ë®Θ


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

Explanation:

ε            # 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)


# J, 27 23 bytes

-4 bytes thanks to Bubbler!

;@(<@({.\);.1~1,2</\}.)


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!

{,/&\'|\'(0,&1_>':x)_y}


Try it online!

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

# APL (Dyalog Extended), 16 bytes

∊⊣⊣\¨⍤⊂⍨1,1↓2</⊢


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


# JavaScript (ES6),  40  39 bytes

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

C=>D=>D.map(c=>C[i]<C[++i]?p=c:p,i=p=0)


Try it online!

# 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!

# Explanation

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.


# 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.

### Explanation

• 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.
• Why is there "(pre-release)" behind "Python 3.8"? Python 3.8 was released months ago. – Mast Mar 23 at 15:05
• @Mast true, I just copied the title from TIO and didn't think much about it – 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.

Explanation:

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


# Charcoal, 22 bytes

≔0ζＦＬη«Ｆ‹§θι§θ⊕ι≔§ηιζζ


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

≔0ζ


Initialise Q.

ＦＬη«


Loop over the elements of D.

Ｆ‹§θι§θ⊕ι


Did CLK transition from 0 to 1?

≔§ηιζ


If so then set Q to D.

ζ


Output Q.

# C (gcc), 80 67 bytes

t;f(c,d,n)int*c,*d;{for(t=*c++;n--;d++)*c++=t^*c&&(t^=1)?*d:c[-1];}


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.