Reduction Operators
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 25번째.
축약 연산자는 하나의 벡터에 있는 모든 비트에 대해 연산을 수행하고, 이를 단일 비트 결과로 축약합니다. 두 수를 비트 단위로 비교하는 비트별 연산자와 달리, 축약 연산자는 하나의 수를 받아 모든 비트에 걸쳐 연산을 수행하여 단일 결과를 생성합니다.
| 연산자 | 연산 | 결과 |
|---|---|---|
& | 리덕션 AND | all bits가 1이면 1 |
| | 리덕션 OR | at least one bit이 1이면 1 |
^ | 리덕션 XOR | odd number의 bits가 1이면 1 |
~& | 리덕션 NAND | all bits가 1이면 0 |
~| | 리덕션 NOR | at least one bit이 1이면 0 |
~^ | 리덕션 XNOR | even number의 bits가 1이면 1 |
작동 방식
감소 AND (<strong>&</strong>):
&4'b1111 = 1 // all bits are 1
&4'b1011 = 0 // 모든 비트가 1이 아님
&4'b0000 = 0 // all bits are 0축약 OR (<strong>|</strong>):
|4'b0000 = 0 // no bits are 1
|4'b0100 = 1 // 적어도 하나의 비트가 1이다
|4'b1111 = 1 // all bits are 1축약 XOR(<strong>^</strong>):
^4'b1010 = 0 // two 1's (even) → 0
^4'b1000 = 1 // one 1 (odd) → 1
^4'b1111 = 0 // 네 개의 1 (짝수) → 0코드 예제
module reduction_demo;
reg [3:0] a, b, c;
reg and_red, or_red, xor_red;
initial begin
a = 4'b1111;
b = 4'b1010;
c = 4'b1000;
and_red = &a; // 1111 → 1
or_red = |b; // 1010 → 1
xor_red = ^c; // 1000 → 1
$display("&4'b1111 = %d", and_red);
$display("|4'b1010 = %d", or_red);
$display("^4'b1000 = %d", xor_red);
$finish;
end
endmodule출력:
&4'b1111 = 1
|4'b1010 = 1
^4'b1000 = 1일반적인 사용
all bits가 1인지 확인:
all_ones = &data; // data == 8'b11111111이면 1어떤 비트가 1인지 확인:
any_one = |data; // data != 0이면 1패리티 확인(1의 odd 개수):
odd_parity = ^data; // 1의 개수가 홀수이면 1모든 비트가 0인지 확인:
all_zeros = ~|data; // data == 0이면 1챌린지
각 작업에 대한 올바른 리덕션 표현식을 작성하세요.
할 일:
a의 모든 비트가 1인지 확인하고 그 결과를all_ones에 저장하세요.b의 비트 중 하나라도 1인지 확인하고 그 결과를any_one에 저장하세요.c에 홀수 개의 1이 있는지 확인하고 그 결과를odd_parity에 저장하세요.
직접 해보기
module reduction_challenge;
reg [3:0] a, b, c;
reg all_ones, any_one, odd_parity;
initial begin
a = 4'b1111;
b = 4'b0100;
c = 4'b1011;
all_ones = ______; // all bits 1?
any_one = ______; // any bit 1?
odd_parity = ______; // odd number of 1's?
$display("&4'b1111 = %d", all_ones);
$display("|4'b0100 = %d", any_one);
$display("^4'b1011 = %d", odd_parity);
$finish;
end
endmodule이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
기초의 모든 레슨
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic직접 연습해 보세요: 온라인 Verilog 컴파일러