Bitwise Operators
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 23번째.
비트 연산자는 숫자의 각 개별 비트에 대해 연산을 수행합니다. 숫자를 전체 값으로 처리하는 산술 연산자(+, -, *, /)와 달리, 비트 연산자는 비트 단위로 작동합니다.
사용 가능한 비트 단위 연산자
| 연산자 | 연산 | 기능 |
|---|---|---|
& | AND | 입력 비트 둘 다 1인 경우에만 출력 비트가 1입니다 |
| | OR | 입력 비트 중 하나 이상이 1이면 출력 비트가 1입니다 |
^ | XOR | 입력 비트가 다르면 출력 비트가 1입니다 |
~ | NOT | 모든 비트를 반전합니다(0은 1이 되고, 1은 0이 됩니다) |
비트 단위 연산자를 사용하면 Verilog는 내부적으로 숫자를 이진수로 처리합니다.
reg [3:0] a, b;
a = 4'd10; // Decimal 10 = binary 1010
b = 4'd12; // 십진수 12 = 이진수 1100
c = a & b; // 이진수를 직접 사용하는 것과 동일하게 동작합니다숫자를 10진수, 16진수 또는 2진수로 작성할 수 있습니다. Verilog는 비트 단위 연산을 위해 이를 2진수로 변환합니다.
비트 단위 연산의 작동 방식
각 연산자는 비트 쌍에 대해 작동합니다(NOT의 경우에는 단일 비트에 대해 작동합니다).
4비트 숫자를 사용한 예:
a = 1010 (binary)
b = 1100 (binary)
AND: a & b = 1000
(1&1=1, 0&1=0, 1&0=0, 0&0=0)
OR: a | b = 1110
(1|1=1, 0|1=1, 1|0=1, 0|0=0)
XOR: a ^ b = 0110
(1^1=0, 0^1=1, 1^0=1, 0^0=0)
NOT: ~a = 0101
(~1=0, ~0=1, ~1=0, ~0=1)코드 예제
module bitwise_demo;
reg [3:0] a, b;
reg [3:0] and_res, or_res, xor_res, not_res;
initial begin
a = 4'b1010;
b = 4'b1100;
and_res = a & b; // 1010 & 1100 = 1000
or_res = a | b; // 1010 | 1100 = 1110
xor_res = a ^ b; // 1010 ^ 1100 = 0110
not_res = ~a; // ~1010 = 0101
$display("a = %b", a);
$display("b = %b", b);
$display("a & b = %b", and_res);
$display("a | b = %b", or_res);
$display("a ^ b = %b", xor_res);
$display("~a = %b", not_res);
$finish;
end
endmodule출력:
a = 1010
b = 1100
a & b = 1000
a | b = 1110
a ^ b = 0110
~a = 0101챌린지
주어진 값에 대한 비트 단위 연산 결과를 계산하세요.
해야 할 일:
a & b를 계산하여and_res에 저장하세요.a | b를 계산하여or_res에 저장하세요.a ^ b를 계산하여xor_res에 저장하세요.~a를 계산하여not_res에 저장하세요.
직접 해보기
module bitwise_challenge;
reg [3:0] a, b;
reg [3:0] and_res, or_res, xor_res, not_res;
initial begin
a = 4'b1101;
b = 4'b1011;
and_res = ______; // a & b
or_res = ______; // a | b
xor_res = ______; // a ^ b
not_res = ______; // ~a
$display("a = %b", a);
$display("b = %b", b);
$display("a & b = %b", and_res);
$display("a | b = %b", or_res);
$display("a ^ b = %b", xor_res);
$display("~a = %b", not_res);
$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 컴파일러