How To Bitshift Properly

This is a discussion on How To Bitshift Properly within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to do some simple bitshifting using bitwise operators. I cannot get it to work as desired. The ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    How To Bitshift Properly

    Hi, I'm trying to do some simple bitshifting using bitwise operators. I cannot get it to work as desired. The blue color gets printed as it should be (19), but the rest are different, this is the output:

    Code:
    red = -86
    green = -24
    blue = 19
    I can't seem to see what I'm doing wrong. When I'm printing the values I shift the bits to the right first to clear the upper bits, then shift it to the bottom. I just noticed that if I use a number up to 128 (not including 128) it works fine. If I use 128 it prints -128, and anything above 128 it prints random garbage. Now, a single byte can store up to 255, so why is it limiting me to 128?!

    Surely it's something simple I'm just missing... Or something I'm doing wrong. Ok thanks, here's the code:

    Code:
    #include <cstdio>
    
    int RgbToInt(int r, int g, int b);
    
    int main(void) {
    	int res = RgbToInt(170, 232, 19);
    	printf("red = %d\n", (res >> 24));
    	printf("green = %d\n",((res << 8) >> 24));
    	printf("blue = %d\n", ((res << 16) >> 24)); 
    }
    
    int RgbToInt(int r, int g, int b) {
    	int rgb = ((r << 24) | (g << 16) | (b << 8));
    	return rgb;
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    Try changing int to unsigned int everywhere, and %d to %u.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Yeah that worked. Is that because with signed integers the first or last bit is used to indicate whether the number is negative or not?
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    Yes. The left most bit indicates the sign and when right shifting, the word may be filled with whatever the bit was--though, I think that's implementation defined.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by lattica View Post
    I think that's implementation defined.
    Yes, it is. Right shifting a signed integer is implementation defined.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. C++ system("rm") not deleting properly?
    By fattysmo in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2008, 11:37 AM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 09:46 PM
  4. How to properly use a .lib file?
    By Kurisu33 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2006, 08:19 PM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21