repl.it
@Koopolt/

peepee

C++

No description

fork
loading
Files
  • main.cpp
  • main
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <bitset>

unsigned short int sign_extend(unsigned short int x, int bit_count)
{
  //0b0000000000010011
  //0b0000000000000001
    if ((x >> (bit_count - 1)) & 1) { //checks first bit of signed integer, resumes if 1.
      std::cout << "signed bit indicates negative. Exterminating minorities.\n";
        x |= (0xFFFF << bit_count);
        //0b0010000000010011
        //0b1111111111100000
    }
    return x;
}

int main() { 
  unsigned short int perp = 0b1101011100110011;
  std::cout << "Hello World!\n";        //         0b1101011100110011
  std::cout <<  std::bitset<16>(sign_extend(perp & 0b0000000000011111, 5));//kinda feel like it could be integrated into the function but whatever
                                            //works by first neglecting digits before the last 5 with a bitwise and statement.
;\
}