Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Down-casting integer
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Vijay Mathew  
View profile  
 More options Oct 6 2012, 3:40 am
Newsgroups: comp.lang.lisp
From: Vijay Mathew <vijay.the.lis...@gmail.com>
Date: Sat, 6 Oct 2012 00:40:13 -0700 (PDT)
Local: Sat, Oct 6 2012 3:40 am
Subject: Down-casting integer
I want to translate the following C code to Common Lisp:

int main()
{
  char a = 255;
  char b = 244;
  char c = (a << 8) | b;
  printf ("%d\n", c); // => -12

}

I tried this:

(logior (ash 255 8) 244) ;; => 65524

The values are treated as integers. How can I get `logior` and `ash` to treat their arguments as bytes and get the same result produced by the C code?

Best regards,

--Vijay  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nils M Holm  
View profile  
 More options Oct 6 2012, 3:52 am
Newsgroups: comp.lang.lisp
From: Nils M Holm <news2...@t3x.org>
Date: 6 Oct 2012 07:52:24 GMT
Local: Sat, Oct 6 2012 3:52 am
Subject: Re: Down-casting integer

(let ((x (mod (logior (ash 255 8) 244) 256)))
  (if (> x 127)
      (- 256 x)
      x))

--
Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Oct 6 2012, 4:07 am
Newsgroups: comp.lang.lisp
From: Kaz Kylheku <k...@kylheku.com>
Date: Sat, 6 Oct 2012 08:07:36 +0000 (UTC)
Local: Sat, Oct 6 2012 4:07 am
Subject: Re: Down-casting integer
On 2012-10-06, Vijay Mathew <vijay.the.lis...@gmail.com> wrote:

> I want to translate the following C code to Common Lisp:

> int main()
> {
>   char a = 255;

Not sure how an "implementation-defined conversion" translates to Common Lisp.
Char may be only 8 bits wide,  and it may be signed, so that it only
goes up to 127.

>   char b = 244;
>   char c = (a << 8) | b;

Shifting a 1 bit into the sign bit of a signed type is undefined behavior.

Here the operand a is promoted to type int. If the value of a is negative,
the behavior is undefined right off the bat. ISO 9899:1999 says:

  The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
  bits are filled with zeros. If E1 has an unsigned type, the value of
  the result is E1 × 2E2 , reduced modulo one more than the maximum
  value representable in the result type. If E1 has a signed type and
  nonnegative value, and E1 × 2E2 is representable in the result type,
  then that is the resulting value; otherwise, the behavior is undefined.
  [6.5.7 paragraph 4]
  [2E2 above is an exponential notation: 2 to the power of E2]

If a is positive (255), then you still have a problem because int might be only
16 bits wide, and so a << 8 will shift a 1 into the sign bit. Undefined
behavior.

>   printf ("%d\n", c); // => -12

So this result is basically nonportable garbage.

> }

> I tried this:

> (logior (ash 255 8) 244) ;; => 65524

Whereas this is mathematically correct.

If you want nonportable garbage in Lisp, you can, oh, modify a list literal!

Or embed a non-externalizable object into source code and compile it.

Or how about: write code that contains some dependency on exactly what exit
points are visible at the point where a dynamic non-local exit is
intercepted to execute some unwind-protect cleanup handlers.

Or apply a huge number of arguments to a function.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vijay Mathew  
View profile  
 More options Oct 6 2012, 4:39 am
Newsgroups: comp.lang.lisp
From: Vijay Mathew <vijay.the.lis...@gmail.com>
Date: Sat, 6 Oct 2012 01:39:11 -0700 (PDT)
Local: Sat, Oct 6 2012 4:39 am
Subject: Re: Down-casting integer
`char` is assumed to be 8-bits wide. The solution of Nils seems to be what I need.

Thanks for helping me out.

--Vijay


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal J. Bourguignon  
View profile  
 More options Oct 6 2012, 5:41 am
Newsgroups: comp.lang.lisp
From: "Pascal J. Bourguignon" <p...@informatimago.com>
Date: Sat, 06 Oct 2012 11:41:14 +0200
Local: Sat, Oct 6 2012 5:41 am
Subject: Re: Down-casting integer

Vijay Mathew <vijay.the.lis...@gmail.com> writes:
> `char` is assumed to be 8-bits wide. The solution of Nils seems to be what I need.

You didn't understand C.

char may be 8 bits wide, your C code is still undefined, while the given
lisp code is perfect defined and sound.  And most C implementations
defined char as signed, so already char a = 255; is undefined.

--
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »