View unanswered posts | View active topics It is currently Sun May 24, 2020 7:33 pm

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Help understanding bitwise shift operator
Author Message
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Help understanding bitwise shift operator
While studying and getting a handle on robotc I encountered this sample code:

 Code:// LiftCtl will be 0, 1, 2 or 3 depending on which buttons are pressedLiftCtl = (vexRT[ LIFT_DN_BUTTON ] << 1) + vexRT[ LIFT_UP_BUTTON ];

I see in help that the << operator is a bitwise shift to the left. And I gather that the result of vexRT[ LIFT_DN_BUTTON ] << 1 will be 1 or 2.
Since the button can only be one or zero I understand:
0<<1 = 1
1<<1 = 2

The help docs explain that a<<b is mathematically equivalent to multiplying by 2^b
so
2^1 = 2

0 * 2 = 0
1 * 2 = 2

That doesn't work so I don't understand. Can someone help explain it to me?

Sat Jul 11, 2015 6:00 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Help understanding bitwise shift operator
Hi there,

0 << 1 is still 0, I'm afraid Here you can see it on a Linux box, using Python:
 Code:xander@ubuntu:~\$ pythonPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 0 << 10>>> 1 << 12>>>

I think you're confusing it with 1 << 0, which is, indeed 1.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Sun Jul 12, 2015 3:15 am
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Re: Help understanding bitwise shift operator
Got it now. One of my early assumptions was wrong, it sent me down the wrong rabbit hole. Thanks again.

Sun Jul 12, 2015 11:42 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 3 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