Question
C CODE The stars are encoded as unsigned integers (32 bits in this case). The first (left-most, most-signicant) 9 bits are to hold an azimuth
C CODE
The stars are encoded as unsigned integers (32 bits in this case).
The first (left-most, most-signicant) 9 bits are to hold an azimuth angle, ranging from 0 to 359 (inclusive). The next 8 bits hold an elevation angle, -90 to 90 (inclusive), in twos complement form, if negative. And that leaves the last 15 bits to hold a brightness from 0 to 2^15 - 1 (32767) (inclusive).
// goal: prints a binary representation of an (unsigned) int
// param x: variable to print the value of
void showbits(unsigned int x) {
for (int i = (8 * sizeof(unsigned int)) - 1; i >= 0; i--) {
putchar(x & (1u << i) ? '1' : '0');
}
printf(" ");
}
// goal: packs an azimuth, elevation, and brightness into a "star"
// param azimuth: desired value of the star's azimuth
// legal range: 0 to 359 (inclusive)
// param elevation: desired value of the star's elevation
// legal range: -90 to 90 (inclusive)
// param brightness: desired value of the star's brightness
// legal range: 0 to 2^15 - 1 (32767) (inclusive)
// psuedo-return *star: variable to pack values into
// 9 most significant bits hold the azimuth
// 8 middle bits hold the elevation (in two's complement)
// 15 least significant bits hold the brightness
// return: error code, -1 if any field outside valid range, 0 otherwise
// (if return -1, *star may take any value, no guarantees)
//
// TODO: Complete the function
int pack(unsigned int* star, int azimuth, int elevation, int brightness)
{
if ((azimuth < 0 && azimuth > 359) || (elevation < -90 && elevation > 90) || (brightness < 0 && brightness > 32767))
{
return -1;
}
else
{
int azi = azimuth << 24;
int ela = 9 >> (~(elevation) + 1) << 15;
int bri = brightness;
int result = azi | ela | bri;
*star = &result;
return 0;
}
}
I need help with this packing
i have tried to use my bitwis logic in my main:
int main()
{
//working on my bits logic here
unsigned int* p_red_Star;
int elevation = 40;//101000 => 1s(010111)(using tiddle) => 2s (011000)
printf("Elevation bits: ");
showbits(elevation << 15);
int azimuth = 200;//11001000
printf("Azimuth bits: ");
showbits(azimuth << 24);
int brightness = 15000;//11101010011000
printf("brightness bits: ");
showbits(brightness);
int ela_2s = ~(elevation) + 1;//11111111111111111111111111-011000
int shift = ela_2s >> 8;// its all 1s
int s_2 = ~(shift);// it will turn into a bunch of zeros
s_2 = s_2 << 8;
//trying to get this: 0000000000000000000000000-011000
//instead i got this: all of bunch of zeros
printf("2s elavation: ");
showbits(s_2); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started