zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Determining the length of "long"?
Date: Thu, 11 Sep 2014 21:39:01 -0700	[thread overview]
Message-ID: <140911213901.ZM21898@torch.brasslantern.com> (raw)
In-Reply-To: <20140911213608.GA1029@gmx.de>

On Sep 11, 10:36pm, Dominik Vogt wrote:
}
} Is there a way to determine the length of the C type long from
} inside a zsh script (without using external programs, of course).

As Kurtis mentioned, the shell really isn't the right tool for this,
but:

if (( ${#:-"$(( [#2] (1<<31)))"} > ${#:-"$(( [#2] (1<<63)))"} ))
then print "zsh integer type is 32 bits"
elif (( ${#:-"$(( [#2] (1<<63)))"} > ${#:-"$(( [#2] (1<<64)))"} ))
then print "zsh integer type is 64 bits"
else print "zsh integer type is more than 64 bits"
fi

} As an alternative, is there a direct way to print out a "long"
} integer value as binary bytes in host byte order?

There's no guarantee that zsh's integer type is "long", so no, there
is not.  You can try fiddling with bitwise operators, e.g.:

setopt C_BASES
integer i=0x12345
while (( i )) {
  printf '\\\\x%x\n' $(( [#16] (i & 0xff) ))
  (( i = i >> 8 ))
}

Printing the zero bytes and not messing up the byte order is left as
an exercise for the reader.


  parent reply	other threads:[~2014-09-12  4:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-11 21:36 Dominik Vogt
2014-09-12  2:58 ` Kurtis Rader
2014-09-12  4:39 ` Bart Schaefer [this message]
2014-09-12  8:42   ` Dominik Vogt
2014-09-12 13:17     ` Kurtis Rader
2014-09-12 23:16     ` Bart Schaefer
2014-09-12 23:42       ` Dominik Vogt
2014-09-13  0:17         ` Bart Schaefer
2014-09-12  8:51   ` Peter Stephenson
2014-09-12 16:17 ` Roman Neuhauser
2014-09-12 17:04   ` Dominik Vogt
2014-09-12 18:10     ` Roman Neuhauser
2014-09-12 18:50       ` Dominik Vogt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140911213901.ZM21898@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).