The Forum » Programming > 0b... and 0x... whats it all about?
Login Register Search Recent Posts

Hamiac

Posted: 6 May 2010 11:28 PM
 



Member Since:
23 August 2008
Posts: 687
Points: 627

This post was awarded 1 point!

Ok I need a bit of help understanding this. When you define ports and stuff you will say; PORTB=0xFF and then you see arrays set up using: 0b00101100 is the 0b different because it uses bits instead of letters? thanks

Acronum

Posted: 7 May 2010 3:37 AM
 



Member Since:
23 October 2007
Posts: 1817
Points: 128

language? chipset? Liam Beale (moderator) http://acronum.com/
============================================================
Liam Beale (moderator) http://www.facebook.com/groups/364570536961945/

Hamiac

Posted: 7 May 2010 3:55 AM
 



Member Since:
23 August 2008
Posts: 687
Points: 627

This post was awarded 2 points!

Sorry, in C. Here is the sample i found: #include #include //declare global arrays for two patterns ; //PB as output PORTB= 0x00; //keep all LEDs off

Phib3r_0ptik

Posted: 7 May 2010 5:24 AM
 


Member Since:
14 October 2007
Posts: 520
Points: 242

This post was awarded 2 points!

The language is obviously C for either AVR or PIC PORTD = 0xd7; is hexdecimal code PORTD = 0b11010111; is the exact same number (215), but in hexdecimal form instead. The two are generally interchangeable in code, but it generally pays to use one convention ----------------- If at first you don't succeed, you must be using Windows.
============================================================
----------------- When Life Gives You Questions, Google has Answers

Microman171

Posted: 7 May 2010 9:43 AM
 


Member Since:
29 August 2008
Posts: 1794
Points: 908

I stick to the hex. For setting up many ports, I prefer to use binary. Binary is easier to read. Dan Collins Moderator
============================================================
Dan 'The Man' Collins Moderator http://dancollins.github.com/

Nitronic755

Posted: 10 May 2010 4:30 AM
 



Member Since:
15 August 2007
Posts: 441
Points: 586

This post was awarded 1 point!

BTW easy way to convert between the two if you understand them. Each hex placeholder will count to 15 decimal (0-9-F) which is equal to a nibble (four bits). To convert binary to hex, break the binary into four block chunks and find what each represents. For example, 916... 1110010100 (Binary) 0011 1001 0100 (Split Binary) 3 9 4 (Decimal Equivalent, and Hex in this case) Of course, if you have a decimal equivalent in the range 10 to 15 then you assign an approprite character, A to F. 255, or 1111 1111, is FF (written as 0xFF under most circumstances) in hex. 256 is 100 in hex. Conversely, you can take each hex character and convert it back to binary. Tom
============================================================
Tom -------------- Need a hand? Flick me a message at nitronic755@gmail.com; I'm happy to help.

Acronum

Posted: 10 May 2010 4:46 AM
 



Member Since:
23 October 2007
Posts: 1817
Points: 128

2b or not 2b = $FF Liam Beale (moderator) http://acronum.com/
============================================================
Liam Beale (moderator) http://www.facebook.com/groups/364570536961945/

Microman171

Posted: 10 May 2010 6:56 PM
 


Member Since:
29 August 2008
Posts: 1794
Points: 908

0x2B | ~(0x2B) == 0xFF; Dan Collins Moderator
============================================================
Dan 'The Man' Collins Moderator http://dancollins.github.com/

Matthew

Posted: 13 June 2010 4:00 AM
 


Member Since:
28 July 2003
Posts: 382
Points: 12

Not sure if this has been covered above, but to clarify: Let's pick a number - in this case 255, the largest number you can store in a char variable (unsigned) or a single byte. - 0xFF in hex - 0b11111111 in decimal Note, you can also represent 16-bit (2-byte values also in this format): ie. 65535 = 2^16 - 0xFFFF - 0b1111111111111111 Often in website development, you may see HTML color tags which look something like this: #FFFFFF -- In this case, this is our Red, Green, Blue (RGB) components in 8-bit resolution (ie. max range 0-255) Matt (Mentor)
============================================================
Matthew Richardson (Elec.& Software Engineer/Mentor, Brightsparks NZ)

William47316

Posted: 14 June 2010 10:03 AM
 



Member Since:
17 May 2005
Posts: 1991
Points: 1767

This post was awarded 1 point!

binary is handy if you forget the decimal value for some things, eg on my eight digit 7 segment display each segment can be turned on or off via binary codes each digit taking an 8 bit byte to control each binary digit is worth 2^x eg 8 bits 2^8 = 256 or 128 64 32 16 8 4 2 1 in each space from 0 - 11111111 a picaxe can use a byte variable to control output pins using a let pins command with a byte variable eg on an 08 or 08M you can count to 0 to 15 in binary in other words 0x... would be hex 0b... would be binary http://resistorhelper.no-ip.org
============================================================
resistorhelper has a new address http://www.resistorhelper.org

You need to be a member to post.