Shop OBEX P1 Docs P2 Docs Learn Events
Bits and bobs: Bits and Bytes — Parallax Forums

Bits and bobs: Bits and Bytes

HughHugh Posts: 362
edited 2009-07-14 12:47 in Propeller 1
Hi,

This is probably a silly question, so feel free to provide a silly answer:

Q: Is there any easy way to convert a byte to bits, i.e., a byte with decimal value '38' to '00100110'?

Bitwise encode isn't quite it. I could write some code to do it, but an indigenous spin function would be much more preferable!

Thanks

Hugh

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.

Comments

  • KyeKye Posts: 2,200
    edited 2009-07-13 18:19
    Um, your talking about strings right?

    If so then:

    VAR
    

    byte binaryCharacters[noparse][[/noparse]33]
    

    PUB numberToBinary(number, length) '' 5 Stack Longs
     
    '' ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
    '' │ Converts an integer number to the binary string of that number padded with zeros.                                        │
    '' │                                                                                                                          │
    '' │ Returns a pointer to the converted string.                                                                               │
    '' │                                                                                                                          │
    '' │ Number - A 32 bit signed integer number to be converted to a string.                                                     │
    '' │ Length - The length of the converted string, negative numbers need a length of 32 for sign extension.                    │
    '' └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
     
      repeat result from 31 to 0
    
        binaryCharacters[noparse][[/noparse]result] := ((number & $1) + "0")
        number >>= 1
     
      return @binaryCharacters[noparse][[/noparse](32 - ((length <# 32) #> 0))]
    

    Otherwise you should realize that '38' and '00100110' are the exact same thing, unless they are strings.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • rokickirokicki Posts: 1,000
    edited 2009-07-13 18:19
    Can you explain more fully what you are trying to do? A "byte" is just a convenient word we use for a
    collection of eight binary bits, so the "conversion" is implicit.

    Are you looking for the ASCII representation of the binary expansion, perhaps?
  • HughHugh Posts: 362
    edited 2009-07-13 18:29
    Sorry.

    I meant that iff I have a byte with a value of '38', can I easily determine the value of Bit 3, Bit 7, Bit 'n', etc., is?

    I think Kye's solution is sufficiently simple and zippy, but I didn't know whether there was a spin single command / function to do the same thing - it would appear not!

    Thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Hugh - the thinking woman's Geoffrey Pyke.
  • KyeKye Posts: 2,200
    edited 2009-07-13 18:43
    Oh, well then my solution will not be useful to you.

    There is no command for what you want to do here.

    ((byte >> n) & 1) is the way to do this.

    As in if I want bit 3 (from bit 7 to bit 0)·then I do.

    ((byte >> 3) & 1)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • JonnyMacJonnyMac Posts: 9,197
    edited 2009-07-13 19:05
    You could code Kye's suggestion into a generalized method:

    pub bittest(value, bit)
      return (value & (1 << bit)) > 0
    


    This returns true if the bit is set, false if it is not.


    [noparse][[/noparse]Edit] If you want to test for multiple bits on, you could do this:

    masktest(value, mask)
      return (value & mask) == mask
    


    So if you want to check if bits 0, 3, and 7 of a value are set you could do this:

    status := masktest(somevar, %10001001)
    

    Post Edited (JonnyMac) : 7/13/2009 7:19:17 PM GMT
  • HughHugh Posts: 362
    edited 2009-07-13 19:10
    More than good enough for me - Mr Lazy!

    burger.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Hugh - the thinking woman's Geoffrey Pyke.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-07-13 19:32
    Here are a couple other ways: one which uses an array, for when the bit number is a variable; the other, using constants, for when the bit number is a constant.

    [b]CON[/b]
    
      [b]_clkmode[/b]      = [b]xtal1[/b] + [b]pll16x[/b]
      [b]_xinfreq[/b]      = 5_000_000
    
      bit0          = |<  0
      bit1          = |<  1
      '...
      bit30         = |< 30 
      bit31         = |< 31
    
    [b]VAR[/b]
    
      [b]long[/b]  bit[noparse][[/noparse]&#173;32&#093;
    
    [b]PUB[/b] Start | i, x
    
      [b]repeat[/b] i [b]from[/b] 0 to 31
        bit[noparse][[/noparse]&#173;i&#093; := |< i
    
      x := $38
      i := 4
      [b]if[/b] (x & bit[noparse][[/noparse]&#173;i&#093;)
        '...
      [b]elseif[/b] (x & bit1)
        '...
    
    
    



    -Phil
  • RaymanRayman Posts: 14,844
    edited 2009-07-13 19:50
    I think a real easy way is use OUTA (assuming that cog isn't really being used for output...).

    You can just do:

    OUTA:=$38

    if OUTA[noparse][[/noparse]bit]
    ...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • ericballericball Posts: 774
    edited 2009-07-14 12:47
    Phil Pilgrim (PhiPi) said...
    Here are a couple other ways: one which uses an array, for when the bit number is a variable; the other, using constants, for when the bit number is a constant.

      [b]if[/b] (x & bit[noparse][[/noparse]&shy;i])
        '...
      [b]elseif[/b] (x & bit1)
        '...
    
    
    

    I'd have to look at the SPIN bytecode, but I suspect "if (x & |<i)" will be faster than "if (x & bit[noparse][[/noparse]i])".· It also saves 32 LONGs of HUB RAM.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Composite NTSC sprite driver: Forum
    NTSC & PAL driver templates: Forum
    OnePinTVText driver: ObEx Forum
Sign In or Register to comment.