There are two ways to pack the bytes in a 16-bit integer:

(Wikipedia The diagrams on the right part way down are clearest explanation to me.)

Update: Switched the descriptions around per proceng++ post below.

  1. Big-endian.

    Here, the bits of the low-value byte are stored in the byte of the target location with the highest address

    You can do this is perl using my $BEbin = pack 'n', 1263;

  2. Little-endian

    Here, the bits of the low-value byte are stored in the byte of the target location with the lowest address.

    You can do this is perl using my $LEbin = pack 'v', 1263;

There are also two ways of unpacking the bits of either representation:

  1. Least significant bit (lsb) first, progressing to most significant bit (msb).

    print unpack 'b16', $bin

  2. msb first progressing to lsb.

    print npack 'B16', $bin;

The result is 4 different ways of displaying the binary representation of a 16-bit number. By the example you gave, it would appear to be the last of those below that you are after:

print unpack 'B16', pack 'v', 1263;; 1110111100000100 print unpack 'B16', pack 'n', 1263;; 0000010011101111 print unpack 'b16', pack 'v', 1263;; 1111011100100000 print unpack 'b16', pack 'n', 1263;; 0010000011110111

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Decmal to 16 bit binary number by BrowserUk
in thread Decmal to 16 bit binary number by isha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.