Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7533925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:52:09+00:00 2026-05-30T05:52:09+00:00

Intput: An integer [0,4095] 12bits. Output: A tuble of {A,B,C} all [0,255] The A,B,C

  • 0

Intput: An integer [0,4095] 12bits.
Output: A tuble of {A,B,C} all [0,255]

The A,B,C are given as 0 to 255, where 255 maps to 15 in the 4 bits. Reason are that I want to construct a Color struct having RGB defined from 0 to 255.

I assume the solution to be something like bit shifting the input to extract the 3 sets of 4bits and then multiply by 17 as (255/15 | 15 = 1111(binary)).

How would you compute this fastest?

my own solution:

QColor mycolor(int value)
{
if(value > 0xFFF)
    value = 0xFFF;

int a=0,b=0,c=0;
a = (value & 0xF) * 17;
b = ((value&(0xF<<4))>>4) *17;
c = ((value&(0xF<<8))>>8) *17;
return QColor(c,b,a);
}



cv::Mat cv_image(10,10,CV_16U,cv::Scalar::all(1));
QImage image(cv_image.data, 10,10,QImage::Format_RGB444);
QPainter p(&image);
p.setPen(mycolor(255));
p.drawLine(0,0,9,0);
p.setPen(mycolor(4095));
p.drawLine(0,1,9,1);
p.setPen(mycolor(0));
p.drawLine(0,2,9,2);
p.setPen(mycolor(10000));
p.drawLine(0,3,9,3);


********* Start testing of Test1 *********
Config: Using QTest library 4.7.4, Qt 4.7.4
PASS   : Test1::initTestCase()
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
  4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095;
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1;
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
PASS   : Test1::test1()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:52:10+00:00Added an answer on May 30, 2026 at 5:52 am

    First of all input 0…4096 is in fact 12 bits and this makes the question easier to understand. Here is one possible solution:

    int val; // 0...4096
    int red =   ((val&(255<<8))>>8)*17;
    int green = ((val&(255<<4))>>4)*17;
    int blue =  ((val&(255<<0))>>0)*17;
    

    I have kept the bit shifting for blue as well so you can spot the similarity in the calculation. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a method that take an integer as input and creates
I want to give integer input to Ruby like this: 12 343 12312 12312
I want to check if user input a positive integer number. 1 = true
I want to take multiple integer inputs in same line eg :- input -1
The Challenge The shortest code by character count that takes a single input integer
I'm writing a program that list the digits of integer input. example: Please enter
I have a very basic question i want to take integer input in certain
Possible Duplicate: Given two arrays a and b .Find all pairs of elements (a1,b1)
I am trying to develope a routine in C# that will take a given
I'm searching for a hash function that takes as input any integer (positive or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.