zsh-users
 help / color / mirror / code / Atom feed
* Bitwise ops in a conditional
@ 2006-04-06 12:07 djh 
  0 siblings, 0 replies; 2+ messages in thread
From: djh  @ 2006-04-06 12:07 UTC (permalink / raw)
  To: zsh


This should be fairly simple, but I missed something.

I want to test if a shell variable is odd or not in a conditional to generate a simple odd/even testing which seems natural to me.  

I did the below:

	  if [[ ((index & 0x01)) .eq 1 ]]  then
	    print "odd index processing"
	  else
	    print "even index processing
	  fi


Actually I thought that a simple
		 if [[ index & 0x01 ]] should work, but conditionaly may requie 2 args?

Any suggestions of using binary operators in conditions would be appreciated.


Thanks,
  Darel Henman


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Bitwise ops in a conditional
@ 2006-04-06  5:43 Dan Nelson
  0 siblings, 0 replies; 2+ messages in thread
From: Dan Nelson @ 2006-04-06  5:43 UTC (permalink / raw)
  To: djh; +Cc: zsh

In the last episode (Apr 06), djh  said:
> This should be fairly simple, but I missed something.
> 
> I want to test if a shell variable is odd or not in a conditional to
> generate a simple odd/even testing which seems natural to me.
> 
> I did the below:
> 
> 	  if [[ ((index & 0x01)) .eq 1 ]]  then
> 	    print "odd index processing"
> 	  else
> 	    print "even index processing
> 	  fi
> 
> Actually I thought that a simple
>
>  if [[ index & 0x01 ]] should work, but conditionaly
>  may requie 2 args?

The if statement doesn't require a conditional expression; all it wants
is something that returns 0 or nonzero.  Use an arithmetic expression
directly (which returns 1 if the expression evaluates to zero, and 0
otherwise, to adhere to the shell's idea of "true is 0, false is 1"):

if (( index & 0x01 )) ; then
  echo odd
else
  echo even
fi

-- 
	Dan Nelson
	dnelson@allantgroup.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-04-06 12:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-06 12:07 Bitwise ops in a conditional djh 
  -- strict thread matches above, loose matches on Subject: below --
2006-04-06  5:43 Dan Nelson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).