Tutorials

OPERATORS PART 3

Shift :

# << left shift
# >> right shift

EXAMPLE :Shift
program main {
reg [3:0] a_1,a_0;
a_1 = 4'b1100 ;
a_0 = 4'b0011 ;

printf(" << by 1 a_1 is %b a_0 is %b \n ",a_1 << 1,a_0 << 1);
printf(" >> by 2 a_1 is %b a_0 is %b \n ",a_1 >> 2,a_0 >> 2);
printf(" >> by 2 a_1 is %b a_0 is %b \n ",a_1 >> 2,a_0 >> 2);
printf(" << by 1 a_1 is %b a_0 is %b \n ",a_1 << 1,a_0 << 1);

}
RESULTS

<< by 1 a_1 is 1000 a_0 is 0110
>> by 2 a_1 is 0011 a_0 is 0000
>> by 2 a_1 is 0011 a_0 is 0000
<< by 1 a_1 is 1000 a_0 is 0110

Bit-Reverse :

>< bit reverse

EXAMPLE : Bit-reverse
program main {
reg [3:0] a_1,a_0;
a_1 = 4'b1100 ;

printf(" >< a_1 is %b \n ",><a_1 );
}
RESULTS

>< a_1 is 0011

Increment And Decrement :

++ increment
-- decrement

EXAMPLE : Increment and Decrement
program main {
integer a_1,a_0;
a_1 = 20 ;
a_0 = 20 ;
a_1 ++ ;
a_0 -- ;
printf (" a_1 is %d a_0 is %d ",a_1,a_0);
}
RESULTS

a_1 is 21 a_0 is 19

Conditional :

?: conditional

EXAMPLE : Conditional
program main {
integer i,j,sub;
for (i=0; i<3; i++)
for (j=3; j>0; j--){
sub = (i>j) ? i-j : j-i;
printf("i = %0d\t j = %0d\tsub = %0d\n", i,j,sub);
}
}
RESULTS

i = 0 j = 3 sub = 3
i = 0 j = 2 sub = 2
i = 0 j = 1 sub = 1
i = 1 j = 3 sub = 2
i = 1 j = 2 sub = 1
i = 1 j = 1 sub = 0
i = 2 j = 3 sub = 1
i = 2 j = 2 sub = 0
i = 2 j = 1 sub = 1

Set

in !in dist

EXAMPLE : Set
program main {
integer i;
i = 20;
if( i in {10,20,30})
printf(" I is in 10 20 30 ");
}
RESULTS

I is in 10 20 30

Replication :

{ number {}}

EXAMPLE : Replication
program main {
reg [5:0] i;
i = { 3{2'b10}};
printf(" I is %b ",i);
}

RESULTS

I is 101010

 << PREVIOUS PAGE TOP NEXT PAGE >>