Question

In: Computer Science

I am trying to create bit masks to create a new binary number with the original...

I am trying to create bit masks to create a new binary number with the original bits' orders turned around, and concataneted.

For example,

int test=0b01100010001010001001(20 bits)

now, I need bit masking to make it:

int result = 20th bit, 10-1 bits, 11th bit, 19-12 bits, in this order.

So in this case, answer would be:

0(20th bit)10100010(10-1 bits)0(11th bit)1011000100(19-12 bits).

Im trying to do something like

int res |= test & 0x7F800;

res |= test& 0x400;

Can't seem to be able to figure out the rest.

Thanks

Solutions

Expert Solution

Greetings!!

Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int test=0b01100010001010001001;
int temp,copy;
printf("given number %x \n \n",test);
temp=test;
copy=test;
temp=test & 0x003ff; //extract 10-1
printf("extracted bits from 10-1 %x \n \n",temp);
temp=temp<<9; //shift to 19-10
printf("shifted to 19-10 %x \n \n",temp);
test=temp & 0x7fe00; //append
printf("appended to 19-10 %x \n \n",test);
temp=copy & 0x00400; //extract 11th bit
printf("extracted bit from 11 %x \n \n",temp);
temp=temp>>2;
printf("shifted to bit 9 %x \n \n",temp);
test=temp | test; //appended 11th bit
printf("appended to 9 %x \n \n",test);
temp=copy & 0x7f800; //extract 19-12 bit
printf("extracted bits from 19-12 %x \n \n",temp);
temp=temp>>11;
printf("shifted to 8-1 %x \n \n",temp);
test=temp | test;
printf("appended to 8-1 %x \n \n",test);

return 0;
}

Output screenshots:

Hope this helps


Related Solutions

I am trying to create an 8-bit random number generator in verilog code using a mux,...
I am trying to create an 8-bit random number generator in verilog code using a mux, a d flip flop and a LFSR not sure what I am doing wrong but need some help with getting it working properly any help would be greatly appreciated. here is what I have so far: module RNG #(parameter size=8)(output [7:0]SO,output [7:0] RN,input clk,rst,input[size-1:0]seed,input L);    wire [7:0] Sin=SO[7]^SO[5]^SO[4]^SO[3];    ffw F1 (SO,clk,rst,Sin);    MUX M1 (Sin,seed,{SO[size-2:0],next},L);    xor X1 (next,SO[6],SO[7]);    assign RN=next;...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am trying to create new empty dataset with a shape (40, 30) in phyton with...
I am trying to create new empty dataset with a shape (40, 30) in phyton with using pandas. How can I create it?
I am trying to write the code for an 8 bit adder in VHDL so that...
I am trying to write the code for an 8 bit adder in VHDL so that I can program it onto my Elbert V2 Spartan 3A FPGA Development Board, but I keep getting errors. Any ideas what I am doing wrong? library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity adder8bit is Port ( a : in STD_LOGIC_VECTOR(7 downto 0); b : in STD_LOGIC_VECTOR(7 downto 0); cin : in STD_LOGIC; o : out STD_LOGIC_VECTOR(7 downto 0); cout : out STD_LOGIC); end adder8bit; architecture Behavioral...
I am trying to create a classified balance sheet and I am unsure what is involved...
I am trying to create a classified balance sheet and I am unsure what is involved when reporting the current assets, liabilities and owners equity?
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
Professor, In trying to apply my knowledge in the real world, I am trying to create...
Professor, In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 22 and 68, in which I would deposit 25% of my income each year. The income starts at 80,000 with an annual growth rate...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT