harm-smits / 42docs

Documentation on MiniLibX and curriculum projects

Home Page:https://harm-smits.github.io/42docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

get_t, get_r and get_g return a wrong value

GlaceCoding opened this issue · comments

#include <stdio.h>

int	create_trgb(int t, int r, int g, int b)
{
	return (t << 24 | r << 16 | g << 8 | b);
}

int	get_t(int trgb)
{
	return (trgb & (0xFF << 24));
}

int	get_r(int trgb)
{
	return (trgb & (0xFF << 16));
}

int	get_g(int trgb)
{
	return (trgb & (0xFF << 8));
}

int	get_b(int trgb)
{
	return (trgb & 0xFF);
}

int main()
{
	int trgb = create_trgb(248, 12, 100, 200);
	printf("%d\n", trgb);
	printf("%d\n", get_t(trgb));
	printf("%d\n", get_r(trgb));
	printf("%d\n", get_g(trgb));
	printf("%d\n", get_b(trgb));

    return 0;
}

output:

-133405496
-134217728
786432
25600
200

Should be:

int	get_t(int trgb)
{
	return ((trgb >> 24) & 0xFF);
}

int	get_r(int trgb)
{
	return ((trgb >> 16) & 0xFF);
}

int	get_g(int trgb)
{
	return ((trgb >> 8) & 0xFF);
}

int main()
{
	int trgb = create_trgb(248, 12, 100, 200);

output:

-133405496
248
12
100
200

resolved by #51