Sign in

Not a member? | Forgot your Password?

Search Comp.Arch.Embedded



Search tips

Free PDF Downloads

Advanced Linux Programming

What Every Programmer Should Know About Memory

Introduction to Embedded Systems

C++ Tutorial

Embedded Systems - Theory and Design Methodology

Recent Blogs on EmbeddedRelated

April is Oscilloscope Month: In Which We Discover Agilent Offers Us a Happy Deal and a Sad Name
posted by Jason Sachs


Little to no benefit from C based HLS
posted by Christopher Felton


Unit Tests for Embedded Code
posted by Stephen Friederichs


DSPRelated and EmbeddedRelated now on Facebook & I will be at EE Live!
posted by Stephane Boucher


Using a RTLSDR dongle to validate NRF905 configuration
posted by fabien le mentec


Introduction to Microcontrollers

Chapter 1: Beginnings

Chapter 2: Further Beginnings

Chapter 3: Hello World

Chapter 4: More On GPIO

Chapter 5: Interrupts

Chapter 6: More On Interrupts

Chapter 7: Timers

Chapter 8: Adding Some Real-World Hardware

Chapter 9: More Timers and Displays

Chapter 10: Buttons and Bouncing

Chapter 11: Button Matrix & Auto Repeating

Chapter 12: Driving WS2812 RGB LEDs

See Also

ElectronicsDSPFPGA

Find us on Facebook





Discussion Groups | Comp.Arch.Embedded | Simple C question...entering binary

There are 46 messages in this thread.

You are currently looking at messages 1 to 10.


So far in April, you have voted 0 times ou of a total of 0 votes by the community.
Please help us clean the archives from unuseful discussion threads by using the voting system! Details here.

Simple C question...entering binary - Thomas Magma - 2008-01-18 13:42:00

Quick question.

How do you enter a literal in binary? That is, the binary equivalent of:

x = 0xFFFF //hex entry

Thank,
Thomas



_____________________________
 Free pdf download: Advanced Linux Programming.

Re: Simple C question...entering binary - Rich Webb - 2008-01-18 14:02:00

Thomas Magma wrote:
> Quick question.
> 
> How do you enter a literal in binary? That is, the binary equivalent of:
> 
> x = 0xFFFF //hex entry

x = 0xFFFF;

I have seen some compilers with non-standard extensions that allow
the similar form

x = 0b11111111;

but that IS non-standard. Probably if one really, really wanted
to do that, the source could be run through a Perl or awk filter
before handing it off to the pre-processor.

Also once saw (and was amazed by) a page of pre-processor macros
that took a "binary" argument and emitted the equivalent hex form.

-- 
Rich Webb     Norfolk, VA

_____________________________
 Free pdf download: Advanced Linux Programming.

Re: Simple C question...entering binary - Thomas Magma - 2008-01-18 14:57:00

>
> x = 0b11111111;
>

The C30 compiler excepted that, thanks. (I'm programming a dsPIC for the 
first time and I like to set my TRIS register values in binary.)

Thanks Rich.




_____________________________
 Free pdf download: Advanced Linux Programming.

Re: Simple C question...entering binary - Stefan Reuther - 2008-01-18 15:08:00

Thomas Magma wrote:
> How do you enter a literal in binary? That is, the binary equivalent of:
> 
> x = 0xFFFF //hex entry

You translate it to hex and use that :-) Or, you define mnemonic names
for the actual bits you're using and use these in an expression, as in
    GCTL = TXEN | RXEN | STOP_BIT | WORD_SIZE_8
instead of
    GCTL = 0b00110101;
(register names have been made up).

That aside, the preprocessor magic version is a variation of
  #define BINARY_OCTET(x) \
    (((0##x & 01)) | \
     ((0##x & 010) >> 2) | \
     ((0##x & 0100) >> 4) | \
     ((0##x & 01000) >> 6) | \
     ((0##x & 010000) >> 8) | \
     ((0##x & 0100000) >> 10) | \
     ((0##x & 01000000) >> 12) | \
     ((0##x & 010000000) >> 14))

  int main() {
    printf("%x\n", BINARY_OCTET(10100100));
    return 0;
  }
It's fun for its hack value, but I wouldn't want it in production code.


  Stefan


_____________________________
 Free pdf download: Introduction to Embedded Systems.

Re: Simple C question...entering binary - Roberto Waltman - 2008-01-18 18:15:00

"Thomas Magma"  wrote:
>How do you enter a literal in binary? That is, the binary equivalent of:
>x = 0xFFFF //hex entry


There is a clever set of macros from Tom Torfs to do this.  See:  


http://www.velocityreviews.com/forums/t317511-binary-constant-macros.html



Roberto Waltman

[ Please reply to the group,
  return address is invalid ]

_____________________________
 Free pdf download: What Every Programmer Should Know About Memory.

Re: Simple C question...entering binary - CBFalconer - 2008-01-18 19:56:00

Thomas Magma wrote:
> 
> How do you enter a literal in binary? That is, the binary
> equivalent of:   x = 0xFFFF //hex entry

You can enter the value in hex, octal, or decimal.  For example:

          unsigned int x;
          ....
          x = 0xffff;  
/* or */  x = 0177777;
/* or */  x = 65535U;
/* or */  x = 0170000 + 0xf00 + 255U;

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com


_____________________________
 Free pdf download: Advanced Linux Programming.

Re: Simple C question...entering binary - 42Bastian Schick - 2008-01-18 23:45:00

On Fri, 18 Jan 2008 21:08:21 +0100, Stefan Reuther
<s...@arcor.de> wrote:

>That aside, the preprocessor magic version is a variation of
>  #define BINARY_OCTET(x) \
>    (((0##x & 01)) | \
>     ((0##x & 010) >> 2) | \
>     ((0##x & 0100) >> 4) | \
>     ((0##x & 01000) >> 6) | \
>     ((0##x & 010000) >> 8) | \
>     ((0##x & 0100000) >> 10) | \
>     ((0##x & 01000000) >> 12) | \
>     ((0##x & 010000000) >> 14))
>
>  int main() {
>    printf("%x\n", BINARY_OCTET(10100100));
>    return 0;
>  }
>It's fun for its hack value, but I wouldn't want it in production code.

Why not. I'd say a decent compiler will just calculate it at compile
time.

-- 
42Bastian
Do not email to b...@yahoo.com, it's a spam-only account :-)
Use <same-name>@monlynx.de instead !

_____________________________
 Free pdf download: Introduction to Embedded Systems.

Re: Simple C question...entering binary - Ulf Samuelsson - 2008-01-19 02:26:00

"Thomas Magma" <s...@overtherainbow.com> skrev i meddelandet 
news:Yw7kj.100328$EA5.10420@pd7urf2no...
> >
>> x = 0b11111111;
>>
>
> The C30 compiler excepted that, thanks. (I'm programming a dsPIC for the 
> first time and I like to set my TRIS register values in binary.)
>
> Thanks Rich.
>
>


And here is the ANSI C compatible way.

#define    BIT(n)    (1 << n)
#define    BIT0        BIT(0)
#define    BIT1        BIT(1)
#define    BIT2        BIT(2)
#define    BIT3        BIT(3)
#define    BIT4        BIT(4)
#define    BIT5        BIT(5)
#define    BIT6        BIT(6)
#define    BIT7        BIT(7)

TRIS = BIT7 | BIT3 | BIT0;

-- 
Best Regards,
Ulf Samuelsson
This is intended to be my personal opinion which may,
or may not be shared by my employer Atmel Nordic AB 



_____________________________
 Free pdf download: Introduction to Embedded Systems.

Re: Simple C question...entering binary - Albert van der Horst - 2008-01-19 11:41:00

In article <4...@yahoo.com>,
CBFalconer  <c...@maineline.net> wrote:
>Thomas Magma wrote:
>>
>> How do you enter a literal in binary? That is, the binary
>> equivalent of:   x = 0xFFFF //hex entry
>
>You can enter the value in hex, octal, or decimal.  For example:
>
>          unsigned int x;
>          ....
>          x = 0xffff;
>/* or */  x = 0177777;
>/* or */  x = 65535U;
>/* or */  x = 0170000 + 0xf00 + 255U;

And the most convenient of all
           x = -1;

(For those not in the know, the definition of unsigned
explicitly makes this works. There may be an additional
advantage that it ports over to 64 bits more easily.)

And of course:

    x = ~0;
>
>--
> [mail]: Chuck F (cbfalconer at maineline dot net)
> [page]: <http://cbfalconer.home.att.net>
>            Try the download section.
>
>
>
>--
>Posted via a free Usenet account from http://www.teranews.com
>


--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

_____________________________
 Free pdf download: Introduction to Embedded Systems.

Re: Simple C question...entering binary - CBFalconer - 2008-01-19 20:36:00

Albert van der Horst wrote:
> CBFalconer  <c...@maineline.net> wrote:
>> Thomas Magma wrote:
>>>
>>> How do you enter a literal in binary? That is, the binary
>>> equivalent of:   x = 0xFFFF //hex entry
>>
>> You can enter the value in hex, octal, or decimal.  For example:
>>
>>           unsigned int x;
>>           ....
>>           x = 0xffff;
>> /* or */  x = 0177777;
>> /* or */  x = 65535U;
>> /* or */  x = 0170000 + 0xf00 + 255U;
> 
> And the most convenient of all
>            x = -1;
> 
> (For those not in the know, the definition of unsigned
> explicitly makes this works. There may be an additional
> advantage that it ports over to 64 bits more easily.)

Depends on the objective.  x = -1 will create the maximum unsigned
value possible (which might be 0xffffffff on some machines).  The
sequences I showed will create 0xffff, regardless of the size of
the int.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com


_____________________________
 Free pdf download: Advanced Linux Programming.

| 1 | | 3 | 4 | 5 |