View unanswered posts | View active topics It is currently Mon Apr 23, 2018 2:00 am

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Protoboard
Author Message
Rookie

Joined: Wed Dec 01, 2010 5:15 pm
Posts: 31
Protoboard

Wed Oct 05, 2011 2:07 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Protoboard
Can you be more specific what you want to be explained?

Wed Oct 05, 2011 6:36 pm
Rookie

Joined: Wed Dec 01, 2010 5:15 pm
Posts: 31
Re: Protoboard
I want to understand how to use bit masks to read digital values from the protoboard.

Mon Oct 24, 2011 8:53 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Protoboard
To understand bit-wise operation, you must first understand hexadecimal number system. Computers only understand binary number system (base 2) because they only know 1's and 0's. The right most digit represents either a 0 or a 1, every subsequent digit to the left is an increment of the power of 2. So if the right most digit is 1, it represents one. If the next digit to the left is a 1, it represents 2. The next digit to the left represents 4, the next 8, the next 16 and so on. Therefore, the following decimal number can be translated to binary.
 Code:(base-10 number) 100 = (64 + 32 + 4) = (base-2 number) 01100100right-most digit = 1*0 = 0next left digit  = 2*0 = 0next left digit  = 4*1 = 4next left digit  = 8*0 = 0next left digit  = 16*0 = 0next left digit  = 32*1 = 32next left digit  = 64*1 = 64next left digit  = 128*0 = 0

But binary numbers are not friendly to human because representing a typical number may require way too many digits. Hexadecimal system (base 16) is a good compromise because 16 is a power of 2 so it can be easily translated between binary and hexadecimal because it uses fewer digits. Every 4 binary bits can be combined to one hexadecimal digit. As the name implies a hexadecimal has 16 possible values (from 0 to 15). To represent the digit in a single character, we added letters (a-f) to represent 10-15. So the number 100 from the above example can be represented as:
 Code:(base-10) 100 = (base 2) 0110 0100 = (base 16) 0x64The (hex) 64 value comes from splitting the base-2 number into two nibbles (4-bit each). So 0110 yields 6=2*1 + 4*1 and 0100 yields 4=4*1

To manipulate bits, you must understand the following bit-wise operators:
- "&" This is to do a bit-wise "AND" of two binary numbers. Typically, it allows you to "clear" a bit in a variable. For example:
 Code:int num1 = 0x64;    //decimal 100 or hex 64 or binary 0110 0100To clear the 3rd bit from the right, you will do the following:int num2 = num1 & 0xfb; //0110 0100 & 1111 1011When two bits "&" together, it will yield a "1" only if both operand bits are "1". So essentially, if you want to leave a bit alone "&" it with a "1". If you want to clear a bit, "&" it with a "0".

- "|" This is to do a bit-wise "OR" of two binary numbers. Typically, it allows you to "set" a bit in a variable. For example:
 Code:int num1 = 0x64;    //decimal 100 or hex 64 or binary 0110 0100To set the 2nd bit from the right, you will do the following:int num2 = num1 | 0x02; //0110 0100 | 0000 0010When two bits "|" together, it will yield a "1" if any operand bits are "1". So essentially, if you want to leave a bit alone "|" it with a "0". If you want to set a bit, "|" it with a "1".

- "~" This is to do a bit-wise "NEGATE" of a binary number. Typically, it allows you to negate a value to form a bit-mask for clearing a bit. For example:
 Code:int num1 = 0x64;    //decimal 100 or hex 64 or binary 0110 0100The third bit from the right is 0x04 (0000 0100)By negating it, you change all the 0's to 1's and all the 1's to 0's.~(0000 0100) = 1111 1011To clear the 3rd bit from the right, you will do the following:int num2 = num1 & ~0x04

- "^" This is to do a bit-wise "EXCLUSIVE-OR". Typically, it allows you to flip a bit. For example:
 Code:int num1 = 0x64;    //decimal 100 or hex 64 or binary 0110 0100If we want to flip the bits of the lower nibble (0100), we do the following:int num2 = 0x64 ^ 0x0f;    //0110 0100 ^ 0000 1111This will give you 0110 1011.When two bits "^" together, it will yield a "1" only if the two operand bits are different. It will yield a "0" if the operand bits are the same. So essentially, if you want to leave a bit alone "^" it with a "0". If you want to flip a bit, "^" it with a "1".

Mon Oct 24, 2011 2:54 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 4 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning