Shifting a byte to the left by 2? - byte

I need to shift a byte left by 2. I am looking at an example for something and he had the byte, 0xC0. And when he shifted it left by 2, he got 0x300. Now, I am trying to take the byte 0xCF and shift it left by 2. Is there any way you could explain how to shift a byte left by two?
Or how to put a simple function together in C# or Java to run this? (Preferably Java).

There are two ways you can shift a byte left by 2, without coding a function. First, you could use this website: http://www.miniwebtool.com/bitwise-calculator/bit-shift/ and it would do it for you. Another way is to multiply by 4. Because shifting left by 2 is the same as multiplying by 4. So 0xCF shifted left 2 is 0x33C.

Related

1 byte that represents different values in java

I have one byte that contains Upper 4 bits are a bitmap for system status like 0x40,0x80 and Lower 4 bits values like 0,1,2,3. I do not know to parse them.Can someone help me.can I have anything like this?how byte will look like???will it be byte b=(byte)0x80?
Bit masking. To get the upper 4 bits:
Upper=byte&0xF0
To get the first
First=byte&0x01
And so on.

LC3 loop through and test left most bit in a word

So I have a word, and I want to loop through and test the left most bit. I have my word and I'm passing it to my subroutine, I know how to build a loop, I'm just not sure how to test the left most bit in the word.
Thanks for any help
The best way to do this is with bit masking -- perform a bitwise AND between the word you want to check and a bit mask with a 1 in any position you wish to test. i.e. in binary:
my word: 11
bitmask: 10
& ==
10
you can see that the 1 in the left side drops out. So to do something similar on a 16bit number:
0x0230 & 0x8000 = 0x0000
0xC020 & 0x8000 = 0x8000 != 0x0000
The important thing to note here is that if the bit is not present the AND returns a 0, and if the bit is present it returns something else. It doesn't matter what it is, just that it's not zero.
Not sure if it applies to your specific task but a simple approach could be performing a logical/arithmetic left shift to the word. This is simply done by adding the word to itself (which is equal to multiplying by 2 and thus shifting all the bits to the left 1 "spot). After doing this, the condition codes will be set (assuming you're using the GPRs) and you can test if the left most bit is a 1 or a 0 by checking if the shifted word is positive OR zero (hence the left most bit is a 0), or negative (hence the left most bit is a 1). Loop over the whole word following this approach and you'll be able to determine the value of each bit in your word. Hope this helps.

crc32 calculating (i'm in despair)

I have been hanging on it too much time...
I've read «A Painless Guide to CRC Error Detection Algorithms» several times. May be I not completely understand theory, but practice seems as clear as sky, but something wrong.
I'm not about code and particular realization, but conceptual (a plain method).
I do this:
1. Take a single byte.
2. Take a uint and fill it with 0xffffffff.
3. Check if the highest bit is 1.
4. Shift one bit to the left.
5. Put the next bit from source byte.
6. It Step3 checking is true, then XOR it with 0x04C11DB7.
7. After data is end, reverse (reflect) working uint.
8. XOR it with 0xffffffff
And it works... but only with zeros (I've checked 1,2,3,4 bytes of zeros). But when I take a byte 0x01 it fails (online calculators show different result). I just can't catch what am I doing wrong.
Step by step (mine version with lowest bit first):
01.Initialization 0xffffffff
02.Shift<< 0fffffffe
03.Place that single 1 0xffffffff
04.XOR 0xfb3ee248
05.Shift<< 0xf67dc490
06.XOR 0xf2bcd927
07.Shift<< 0xe579b24e
08.XOR 0xe1b8aff9
09.Shift<< c3715ff2
10.XOR 0xc7b04245
11.Shift<< 0x8f60848a
12.XOR 8ba1993d
13.Shift<< 0x1743327a
14.XOR 0x13822fcd
15.Shift<< 0x27045f9a
16.Shift<< 0x4e08bf34
17.Reflect 0x2cfd1072
18.XOR (0xffffffff) 0xd302ef8d (the result)
Please help! What is wrong with it?
At last, I've got the reciept. It took much time, but I reinvented it ))
Share it with anyone, who need it:
1. Take first 4 bytes from message (if it less than 4 byte - add zeros). May be you will need to reflect bits in EVERY byte (I have to, but I think it depends on particular architecture). Put it into Register (uint).
2. Make Register XOR 0xFFFFFFFF.
3. Shift one bit left.
4. Place the next message's bit (the lowest one first) to the right side of Register.
5. If shifted bit was 1, than Register XOR 0x04C11DB7.
6. Do steps 3-5 until the end of the message.
7. Do steps 3-5 for 32 bits of zeros (if the message is less than 32 bits, than this number must correspond with input length).
7. Reflect bits in the whole Register.
8. Make Register XOR 0xffffffff.
That's it - you have the CRC32, which all online calculators show and, at least, correct for deflate, PNG, etc.

Reverse order of bits using shift and rotate

I am asked on HW to reverse bits, like mirror flip them so for example 1011 0100 becomes 0010 1101, using shift and rotate combination. I understand how those commands work but I can't think of a way to flip them. Thanks.
I need to do it using SAL assembly language.
If you need to flip a b-bit word using only shifts, you could emulate a stack:
b times{
right shift on the input register, setting a carry flag.
left shift on the output register, reading the carry flag.
}
Note that x86 has the "rotate through carry" instructions - they serve both purposes (or use rotation without carry on the input register to preserve the input). If left shift from carry is not available but right shift from carry is, reverse the words "left" and "right" in the previous algorithm. If no shift from carry is available, you need to emulate by an "ordinary" logical shift followed by setting the correct bit, but...
If you can use AND and OR as well and b is known ahead and is a power of two, there is a faster way. Reverse two bits within each pair, then two pairs within each nibble, then two nibbles within each byte, then two bytes within each word...
for 8-bit x:
//1234 5678
x = (0x55 & x)<< 1 | (0xAA & x)>> 1 //2143 6587
x = (0x33 & x)<< 2 | (0xCC & x)>> 2 //4321 8765
x = (0x0F & x)<< 4 | (0xF0 & x)>> 4 //8765 4321

Right shift using iteration in LC3

So I am working on a program in LC3 and I am having a issue with masking. I am fine with creating a mask of all 1's of the appropriate length, fine shifting everything to the appropriate field, but I can't for the life of me shift things back over to the right once I am done doing the AND comparison. How do you right shift in LC3? I saw something on iteration comparison, but I don't know how to compare bits in different positions, and to compare them in the same position I would have to shift one to the right eventually anyway. Am I missing something?
To simulate right shift, you just need to make two masks, one for source bit and one for destination bit:
src_mask=0x04; // read from bit position 2
dst_mask=0x01; // write to bit position 0
while(src) { // or while src_mask, because that too will eventually go to zero
if (src & src_mask) dst+=dst_mask; // or dst|=dst_mask
src &= (~src_mask);
dst_mask=dst_mask+dst_mask;
src_mask=src_mask+src_mask;
}
You might not need to do a right shift in this situation at all -- just save the starting field before shifting it left, then restore it from memory afterwards.

Resources