unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* C-x C-e on numbers
@ 2014-12-04 15:56 Alfred M. Szmidt
  2014-12-04 16:27 ` Andreas Schwab
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Alfred M. Szmidt @ 2014-12-04 15:56 UTC (permalink / raw)
  To: emacs-devel

Currently, when evaluating a number, emacs will print the decimal,
octal, hexadecimal, and charachter of said number.

  42 C-x C-e ==> 42 (#o52, #x2a, ?*)

It would be useful, if the output would also include the binary
representation of the number.  Or at least somehow enable such
behaviour.  I.e., 

  42 C-x C-e ==> 42 (#b101010, #o52, #x2a, ?*)

It would also be useful to change the printed representation of a
number, so that the above would become,

  #o52 C-x C-e ==> #x2a (#b101010, 42, #o52, ?*)

But this would probobly require some more work.




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

* Re: C-x C-e on numbers
  2014-12-04 15:56 C-x C-e on numbers Alfred M. Szmidt
@ 2014-12-04 16:27 ` Andreas Schwab
  2014-12-04 16:47 ` Stephen Leake
  2014-12-05  7:04 ` Stefan Reichör
  2 siblings, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2014-12-04 16:27 UTC (permalink / raw)
  To: Alfred M. Szmidt; +Cc: emacs-devel

ams@gnu.org (Alfred M. Szmidt) writes:

> Currently, when evaluating a number, emacs will print the decimal,
> octal, hexadecimal, and charachter of said number.
>
>   42 C-x C-e ==> 42 (#o52, #x2a, ?*)
>
> It would be useful, if the output would also include the binary
> representation of the number.  Or at least somehow enable such
> behaviour.  I.e., 
>
>   42 C-x C-e ==> 42 (#b101010, #o52, #x2a, ?*)

Since there is no builtin way to format numbers in binary this requires
some work.  Also, the binary representation of a number can get quite
long.  IMHO it doesn't add much value over the octal representation.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



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

* Re: C-x C-e on numbers
  2014-12-04 15:56 C-x C-e on numbers Alfred M. Szmidt
  2014-12-04 16:27 ` Andreas Schwab
@ 2014-12-04 16:47 ` Stephen Leake
  2014-12-05  6:53   ` Stephen J. Turnbull
  2014-12-05  7:04 ` Stefan Reichör
  2 siblings, 1 reply; 5+ messages in thread
From: Stephen Leake @ 2014-12-04 16:47 UTC (permalink / raw)
  To: emacs-devel

ams@gnu.org (Alfred M. Szmidt) writes:

> Currently, when evaluating a number, emacs will print the decimal,
> octal, hexadecimal, and charachter of said number.
>
>   42 C-x C-e ==> 42 (#o52, #x2a, ?*)
>
> It would be useful, if the output would also include the binary
> representation of the number.  Or at least somehow enable such
> behaviour.  I.e., 
>
>   42 C-x C-e ==> 42 (#b101010, #o52, #x2a, ?*)

I often need to examine binary; currently, I shell out to an Ada
executable that does it for me.

But I'm already annoyed at how many representations are shown, so I'd
rather just make it available; I can always do

M-: (show-binary 42)

or something similar.

gdb uses the format letter "t" for binary; ideally, I'd like to see that
in elisp format; then I could do:

(format "%t" 42) ==> #b101010

That covers all the possible combinations of input and output format.   

But I suspect that means adding it to the underlying standard C library,
which isn't going to happen.

-- 
-- Stephe



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

* Re: C-x C-e on numbers
  2014-12-04 16:47 ` Stephen Leake
@ 2014-12-05  6:53   ` Stephen J. Turnbull
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen J. Turnbull @ 2014-12-05  6:53 UTC (permalink / raw)
  To: Stephen Leake; +Cc: emacs-devel

Stephen Leake writes:

 > M-: (show-binary 42)

(defun show-binary (n)
  "Documentation left as exercise for the reader."
  ;; hexadecimal version also an exercise
  (let ((s (format "%o" n)))
    ;; loop unrolled for creativity conservation
    (setq s (replace-in-string s "0" "000"))
    (setq s (replace-in-string s "1" "001"))
    (setq s (replace-in-string s "2" "010"))
    (setq s (replace-in-string s "3" "011"))
    (setq s (replace-in-string s "4" "100"))
    (setq s (replace-in-string s "5" "101"))
    (setq s (replace-in-string s "6" "110"))
    (setq s (replace-in-string s "7" "111"))
    ;; stripping leading zeros or padding to wordsize, more homework
    (concat "#b" s)))

Seriously, does this function really need to be particularly efficient?




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

* Re: C-x C-e on numbers
  2014-12-04 15:56 C-x C-e on numbers Alfred M. Szmidt
  2014-12-04 16:27 ` Andreas Schwab
  2014-12-04 16:47 ` Stephen Leake
@ 2014-12-05  7:04 ` Stefan Reichör
  2 siblings, 0 replies; 5+ messages in thread
From: Stefan Reichör @ 2014-12-05  7:04 UTC (permalink / raw)
  To: emacs-devel

ams@gnu.org (Alfred M. Szmidt) writes:

> Currently, when evaluating a number, emacs will print the decimal,
> octal, hexadecimal, and charachter of said number.
>
>   42 C-x C-e ==> 42 (#o52, #x2a, ?*)
>
> It would be useful, if the output would also include the binary
> representation of the number.  Or at least somehow enable such
> behaviour.  I.e., 
>
>   42 C-x C-e ==> 42 (#b101010, #o52, #x2a, ?*)

M-x quick-calc <RET> 42 <RET> ==> Result: 42 =>  42  (16#2A, 8#52, 2#101010, "*")

> It would also be useful to change the printed representation of a
> number, so that the above would become,
>
>   #o52 C-x C-e ==> #x2a (#b101010, 42, #o52, ?*)
>
> But this would probobly require some more work.

M-x quick-calc <RET> 8#52 <RET> ==> Result: 42 =>  42  (16#2A, 8#52, 2#101010, "*")


calc handles this and a lot more...

Stefan.




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

end of thread, other threads:[~2014-12-05  7:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-04 15:56 C-x C-e on numbers Alfred M. Szmidt
2014-12-04 16:27 ` Andreas Schwab
2014-12-04 16:47 ` Stephen Leake
2014-12-05  6:53   ` Stephen J. Turnbull
2014-12-05  7:04 ` Stefan Reichör

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

	https://git.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).