472,328 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

bitshifting help

Okay, so I have a 16 bit positive value (0-65535) representing a color in 565 RGB notation, that is 5 bits for red, 6 bits for green, and 5 bits for blue.

I need to separate the red, green, and blue values out so I may give them to a function that takes red, green, and blue values separately.

So, thinking back to C, I figured I could use bitshifts. But when I do, it appears the bits wrap around. So is this even possible in Python?
Nov 20 '06 #1
8 1586
bartonc
6,596 Expert 4TB
Okay, so I have a 16 bit positive value (0-65535) representing a color in 565 RGB notation, that is 5 bits for red, 6 bits for green, and 5 bits for blue.

I need to separate the red, green, and blue values out so I may give them to a function that takes red, green, and blue values separately.

So, thinking back to C, I figured I could use bitshifts. But when I do, it appears the bits wrap around. So is this even possible in Python?
One way is (off the top of my head)
Expand|Select|Wrap|Line Numbers
  1.  
  2. rgbValue = 65535
  3. red = rgbValue & 0xf800 >> 11
  4. green = rgbValue & 0x07e0 >> 5
  5. blue = rgbValue & 0x001f 
  6.  
I'm sure I'll think of others when the pressure is off.
Nov 20 '06 #2
bartonc
6,596 Expert 4TB
By the way, spacecoyote, welcome to TheScripts! We're glad you found the site and hope that you post often and tell your friends about us.
Nov 20 '06 #3
One way is (off the top of my head)
Expand|Select|Wrap|Line Numbers
  1.  
  2. rgbValue = 65535
  3. red = rgbValue & 0xf800 >> 11
  4. green = rgbValue & 0x07e0 >> 5
  5. blue = rgbValue & 0x001f 
  6.  
I'm sure I'll think of others when the pressure is off.
Unfortunately, that doesn't seem to work :(

If I substitute (for example) the value 43039, r, g, and b still equal 31, so it apparently only works for 65535...
Nov 20 '06 #4
What values are you expecting? 31 is 011111 (5 bits all one).
Hmm...

43039 =
1010100000011111

so:

r=10101=21
g=000000=0
b=11111=31
Nov 20 '06 #5
I see that shifting doesn't work for some reason. I'll research this and get back to you.
The AND part works, so for now you can just divide by the appropriate power of 2
r = c >> 11 works and
b = c & 0x001f works,
g is the tough one...
Nov 20 '06 #6
bartonc
6,596 Expert 4TB
r = c >> 11 works and
b = c & 0x001f works,
g is the tough one...
Boy do I feel foolish. It's operator presidence:
(43039 & 0x07e0) >> 5
Nov 20 '06 #7
Boy do I feel foolish. It's operator presidence:
(43039 & 0x07e0) >> 5
Hooray! It works. Thank you :)
Nov 20 '06 #8
bartonc
6,596 Expert 4TB
Hooray! It works. Thank you :)
You are quite welcome. Post any time.
Nov 20 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an...
10
by: Jacek Generowicz | last post by:
Where can I find concise, clear documentation describing what one has to do in order to enable Python's internal help to be able to provide...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright",...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more:...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.