Additional condition in IF statement doesn't work

K

kb33

Guest
Hi,

I have two pieces of code, one works, the other doesn't. I am not able
to figure out why the second one doesn't. Any insight would be
appreciated. The second piece of code has an additional condition in
the IF statement

//Definitions common to both pieces of code:

reg [4:0] data_array [0:3];


reg [1:0] index_array1 [0:5],
index_array2 [0:5];

reg [1:0] var_1;

integer i;


initial
begin
//Initialization....

var_1 <= 2;

index_array1 [0] <= 0;
index_array1 [1] <= 2;

index_array2 [0] <= 1;
index_array2 [1] <= 3;


data_array [0] <= 8;
data_array [1] <= 4;
data_array [2] <= 3;
data_array [3] <= 1;


//This piece of code works:


for (i=0; i<2; i=i+1)
if (data_array [index_array1 ] > data_array
[index_array2 ])
begin
data_array [index_array1] <= data_array
[index_array2];
data_array [index_array2] <= data_array
[index_array1];
end


//This piece of code doesn't work:

for (i=0; i<2; i=i+1)
begin

if ((var_1 & index_array1 == 0) && (data_array
[index_array1 ] > data_array [index_array2]))
begin
data_array [index_array1 ] <= data_array
[index_array2 ];
data_array [index_array2 ] <= data_array
[index_array1 ];
end

else

if ((var_1 & index_array1 != 0) && (data_array
[index_array1 ] < data_array [index_array2]))
begin
data_array [index_array1 ] <= data_array
[index_array2 ];
data_array [index_array2 ] <= data_array
[index_array1 ];
end

end
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


You might want to look up in your books the difference between
the bitwise and '&' operator and the logical and '&&' operator.
I think you will find enlightenment there. I'm thinking specifically
about this bit of your code: (var_1 & index_array1 == 0)

kb33 wrote:
Hi,

I have two pieces of code, one works, the other doesn't. I am not able
to figure out why the second one doesn't. Any insight would be
appreciated. The second piece of code has an additional condition in
the IF statement



//This piece of code doesn't work:

for (i=0; i<2; i=i+1)
begin

if ((var_1 & index_array1 == 0) && (data_array
[index_array1 ] > data_array [index_array2]))
begin
data_array [index_array1 ] <= data_array
[index_array2 ];
data_array [index_array2 ] <= data_array
[index_array1 ];
end


- --
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,
http://www.icarus.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFGTg8lrPt1Sc2b3ikRAqrOAJ9ztz78y92JG3hlX8DWBbpZ9/ShbQCg41cQ
Wfshk7iThO0BkbR4wBAboz8=
=Vtrj
-----END PGP SIGNATURE-----
 
On May 18, 4:40 pm, Stephen Williams <spamt...@icarus.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You might want to look up in your books the difference between
the bitwise and '&' operator and the logical and '&&' operator.
I think you will find enlightenment there. I'm thinking specifically
about this bit of your code: (var_1 & index_array1 == 0)


I think I am clear about what I wish to do - the first condition
checks if the bitwise operation leads to a zero (or not), and the &&
checks for the Logical AND operation. The text book says just as much.



kb33 wrote:
Hi,

I have two pieces of code, one works, the other doesn't. I am not able
to figure out why the second one doesn't. Any insight would be
appreciated. The second piece of code has an additional condition in
the IF statement

//This piece of code doesn't work:

for (i=0; i<2; i=i+1)
begin

if ((var_1 & index_array1 == 0) && (data_array
[index_array1 ] > data_array [index_array2]))
begin
data_array [index_array1 ] <= data_array
[index_array2 ];
data_array [index_array2 ] <= data_array
[index_array1 ];
end

- --
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,http://www.icarus.com and lines to code before I sleep,http://www.picturel.com And lines to code before I sleep."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE -http://enigmail.mozdev.org

iD8DBQFGTg8lrPt1Sc2b3ikRAqrOAJ9ztz78y92JG3hlX8DWBbpZ9/ShbQCg41cQ
Wfshk7iThO0BkbR4wBAboz8=
=Vtrj
-----END PGP SIGNATURE-----
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Try an extra set of () around your '&' expression. It is perhaps not
grouping the expression the way you think?


kb33 wrote:
I think I am clear about what I wish to do - the first condition
checks if the bitwise operation leads to a zero (or not), and the &&
checks for the Logical AND operation. The text book says just as much.



kb33 wrote:
Hi,
I have two pieces of code, one works, the other doesn't. I am not able
to figure out why the second one doesn't. Any insight would be
appreciated. The second piece of code has an additional condition in
the IF statement
//This piece of code doesn't work:
for (i=0; i<2; i=i+1)
begin
if ((var_1 & index_array1 == 0) && (data_array
[index_array1 ] > data_array [index_array2]))
begin
data_array [index_array1 ] <= data_array
[index_array2 ];
data_array [index_array2 ] <= data_array
[index_array1 ];
end
--
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,http://www.icarus.com and lines to code before I sleep,http://www.picturel.com And lines to code before I sleep."

- --
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,
http://www.icarus.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFGThsmrPt1Sc2b3ikRAiuxAKCrLHRZb83C83VNYYSDpIrqoOdhDACgiipo
7BobkRiEspEIEtHbiKYX77U=
=bij1
-----END PGP SIGNATURE-----
 
On May 19, 2:31 am, Stephen Williams <spamt...@icarus.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Try an extra set of () around your '&' expression. It is perhaps not
grouping the expression the way you think?



kb33 wrote:
I think I am clear about what I wish to do - the first condition
checks if the bitwise operation leads to a zero (or not), and the &&
checks for the Logical AND operation. The text book says just as much.

kb33 wrote:
Hi,
I have two pieces of code, one works, the other doesn't. I am not able
to figure out why the second one doesn't. Any insight would be
appreciated. The second piece of code has an additional condition in
the IF statement
//This piece of code doesn't work:
for (i=0; i<2; i=i+1)
begin
if ((var_1 & index_array1 == 0) && (data_array
[index_array1 ] > data_array [index_array2]))
begin
data_array [index_array1 ] <= data_array
[index_array2 ];
data_array [index_array2 ] <= data_array
[index_array1 ];
end
--
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,http://www.icarus.com and lines to code before I sleep,http://www.picturel.com And lines to code before I sleep."

- --
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,http://www.icarus.com and lines to code before I sleep,http://www.picturel.com And lines to code before I sleep."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE -http://enigmail.mozdev.org

iD8DBQFGThsmrPt1Sc2b3ikRAiuxAKCrLHRZb83C83VNYYSDpIrqoOdhDACgiipo
7BobkRiEspEIEtHbiKYX77U=
=bij1
-----END PGP SIGNATURE-----

Hi!
It is not a better to rely on the precedence of operators to resolve a
equation,
As Steve mentioned it, please use () around the equation which makes
the code
easier to read and does not rely on operators precedence.

Rajkumar...
 

Welcome to EDABoard.com

Sponsor

Back
Top