Modulo Operator
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 20번째.
또 다른 기본 산술 연산자는 나머지 연산자(%)입니다. 나눗셈 후의 나머지를 제공합니다.
한 수를 다른 수로 나눌 때 나머지 연산자는 무엇이 남는지 알려 줍니다.
| 식 | 결과 | 설명 |
|---|---|---|
10 % 3 | 1 | 10 ÷ 3 = 3, 나머지 1 |
15 % 4 | 3 | 15 ÷ 4 = 3, 나머지 3 |
8 % 2 | 0 | 8 ÷ 2 = 4, 나머지 0 |
코드 예제
module modulo_demo;
reg [7:0] result;
initial begin
result = 10 % 3;
$display("10 %% 3 = %d", result); // 1
result = 15 % 4;
$display("15 %% 4 = %d", result); // 3
result = 8 % 2;
$display("8 %% 2 = %d", result); // 0
$finish;
end
endmodule일반적인 용도
숫자가 짝수인지 홀수인지 확인:
if (number % 2 == 0)
$display("Even");
else
$display("Odd");카운터 순환시키기(0에서 9까지 센 다음 다시 0으로 돌아가기):
count = (count + 1) % 10;숫자의 last digit 가져오기:
last_digit = value % 10; // 123 % 10 = 3챌린지
각 식의 나머지를 계산하세요.
할 일:
17 % 5를 계산하고mod1에 저장하세요22 % 7을 계산하고mod2에 저장하세요14 % 4를 계산하고mod3에 저장하세요
직접 해보기
module modulo_challenge;
reg [7:0] mod1, mod2, mod3;
initial begin
mod1 = ______; // 17 % 5
mod2 = ______; // 22 % 7
mod3 = ______; // 14 % 4
$display("17 %% 5 = %d", mod1);
$display("22 %% 7 = %d", mod2);
$display("14 %% 4 = %d", mod3);
$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 컴파일러