Loop storing 1s complement - binary

Suppose a computer uses 4-bit one’s complement numbers. Ignoring overflows, what value will be stored in the variable j after the following pseudocode routine terminates?
0 → j // Store 0 in j
-3 → k // Store -3 in k
while k ≠ 0
j = j + 1
k = k - 1
end while
'''
I thought the loop would continue forever, but the answer in the back of the book says:
J (Binary) K (Binary)
0 0000 -3 1100
1 0001 -4 1011 (1100 + 1110) (where last carry is added to sum doing 1's complement addition)
2 0010 -5 1010 (1011 + 1110)
3 0011 -6 1001 (1010 + 1110)
4 0100 -7 1000 (1001 + 1110)
5 0101 7 0111 (1000 + 1110) (This is overflow -- but you can ignore)
6 0110 6 0110
7 0111 5 0101
-7 1000 4 0100
-6 1001 3 0011
-5 1010 2 0010
-4 1011 1 0001
-3 1100 0 0000
I do not understand how the k can flip to positives and the j to negatives. I do understand how they got the rows of where j =0,1,2,3,4

Related

Binary subset rank with maximum k consecutive 0

Consider N = 6 bits
We have a set S = 2^6 = 64 binary combinations.
We want to consider a subset T of the set S based on the following criteria -
There must be <= k consecutive 0s in the subset T.
Accordingly,
If the binary number is present in subset T, calculate its rank (in binary format)
If the binary number is not present in subset T, return 0 (in binary format)
Note: We want to calculate the rank only by iterating the N bits of the given binary number, and NOT by iterating 2^N numbers.
Here is the pseudo-code -
string GetBinaryRank(string strBinaryNumber, int max_k_0s)
{
string strRankBinary = "";
if(strBinaryNumber.Contains(<= max_k_0s))
{
//calculate strRankBinary by iterating over the N bits of strBinaryNumber
for(int i = 0; i < strBinaryNumber.Length; i++)
{
if(max_k_0s == 1)
{
//calculate rank
//binary rank will be 5 bits
strRankBinary = "xxxxx";
}
else if(max_k_0s == 2)
{
//calculate rank
//binary rank will be 6 bits
strRankBinary = "yyyyyy";
}
}
}
else
{
strRankBinary = "000000";
}
return strRankBinary;
}
We want to do the rank and unrank operations for the binary number, and determine the maximum number of bits required for "strRankBinary"
Also, we want to generalize the code for "N"-bit numbers and maximum "k" consecutive 0s.
Depending on the maximum rank value, the strRankBinary must contain only the required number of maximum bits.
In the examples given below,
If max_k_0s = 1, maximum rank will be 21, which is 10101 (5 bits) in
binary.
If max_k_0s = 2, maximum rank will be 44, which is 101100 (6 bits) in
binary.
Example 1 (max 1 consecutive 0s in 6-digit binary value):
Number Rank Binary rank
-------------------------------
000000 0 00000
000001 0 00000
000010 0 00000
000011 0 00000
000100 0 00000
000101 0 00000
000110 0 00000
000111 0 00000
001000 0 00000
001001 0 00000
001010 0 00000
001011 0 00000
001100 0 00000
001101 0 00000
001110 0 00000
001111 0 00000
010000 0 00000
010001 0 00000
010010 0 00000
010011 0 00000
010100 0 00000
010101 1 00001
010110 2 00010
010111 3 00011
011000 0 00000
011001 0 00000
011010 4 00100
011011 5 00101
011100 0 00000
011101 6 00110
011110 7 00111
011111 8 01000
100000 0 00000
100001 0 00000
100010 0 00000
100011 0 00000
100100 0 00000
100101 0 00000
100110 0 00000
100111 0 00000
101000 0 00000
101001 0 00000
101010 9 01001
101011 10 01010
101100 0 00000
101101 11 01011
101110 12 01100
101111 13 01101
110000 0 00000
110001 0 00000
110010 0 00000
110011 0 00000
110100 0 00000
110101 14 01110
110110 15 01111
110111 16 10000
111000 0 00000
111001 0 00000
111010 17 10001
111011 18 10010
111100 0 00000
111101 19 10011
111110 20 10100
111111 21 10101
Example 2 (max 2 consecutive 0s in 6-digit binary value):
Number Rank Binary rank
-------------------------------
000000 0 000000
000001 0 000000
000010 0 000000
000011 0 000000
000100 0 000000
000101 0 000000
000110 0 000000
000111 0 000000
001000 0 000000
001001 1 000001
001010 2 000010
001011 3 000011
001100 4 000100
001101 5 000101
001110 6 000110
001111 7 000111
010000 0 000000
010001 0 000000
010010 8 001000
010011 9 001001
010100 10 001010
010101 11 001011
010110 12 001100
010111 13 001101
011000 0 000000
011001 14 001110
011010 15 001111
011011 16 010000
011100 17 010001
011101 18 010010
011110 19 010011
011111 20 010100
100000 0 000000
100001 0 000000
100010 0 000000
100011 0 000000
100100 21 010101
100101 22 010110
100110 23 010111
100111 24 011000
101000 0 000000
101001 25 011001
101010 26 011010
101011 27 011011
101100 28 011100
101101 29 011101
101110 30 011110
101111 31 011111
110000 0 000000
110001 0 000000
110010 32 100000
110011 33 100001
110100 34 100010
110101 35 100011
110110 36 100100
110111 37 100101
111000 0 000000
111001 38 100110
111010 39 100111
111011 40 101000
111100 41 101001
111101 42 101010
111110 43 101011
111111 44 101100

How to understand IR checksum

I'm trying to understand how crc is calculated of below data packets of unbranded air con remote controller. it's last 4 bits seem to be crc and data packet is 8 bytes long. I can understand how to extract the data packet but not crc. First 3 bits are fixed.
data pattern eg 1) 0010 0100 0100 0011 0000 0000 0000 0000 0000 0000 1110
data pattern eg 2) 0010 1000 0101 0011 0000 0000 0000 0000 0000 0000 1101
data pattern eg 3) 0010 1000 0110 0011 0000 0000 0000 0000 0000 0000 1100
data pattern eg 4) 0010 1000 0111 0011 0000 0000 0000 0000 0000 0000 1011
data pattern eg 5) 0010 1000 1000 0011 0000 0000 0000 0000 0000 0000 1010
data pattern eg 6) 0010 1000 1001 0011 0000 0000 0000 0000 0000 0000 1001
data pattern eg 7) 0010 1000 1010 0011 0000 0000 0000 0000 0000 0000 1000
data pattern eg 8) 0010 1000 1011 0011 0000 0000 0000 0000 0000 0000 0111
data pattern eg 9) 0010 1000 0000 0011 0000 0000 0000 0000 0000 0000 0010
data pattern eg 10) 0010 1000 0001 0011 0000 0000 0000 0000 0000 0000 0001
data pattern eg 11) 0010 1000 0010 0011 0000 0000 0000 0000 0000 0000 0000
data pattern eg 12) 0010 1000 0011 0011 0000 0000 0000 0000 0000 0000 1111
sample image
I think this might be just an inverted sum.
Adding up all but last 4 bits, and then ((x&1111)^1111):
0010 + 0100 + 0100 + 0011 = 1101 -> 0010*
0010 + 1000 + 0101 + 0011 = 10010 -> 1101
0010 + 1000 + 0110 + 0011 = 10011 -> 1100
0010 + 1000 + 0111 + 0011 = 10100 -> 1011
0010 + 1000 + 1000 + 0011 = 10101 -> 1010
0010 + 1000 + 1001 + 0011 = 10110 -> 1001
0010 + 1000 + 1010 + 0011 = 10111 -> 1000
0010 + 1000 + 1011 + 0011 = 11000 -> 0111
0010 + 1000 + 0000 + 0011 = 1101 -> 0010
0010 + 1000 + 0001 + 0011 = 1110 -> 0001
0010 + 1000 + 0010 + 0011 = 1111 -> 0000
0010 + 1000 + 0011 + 0011 = 10000 -> 1111
Note that first sample is incorrect (should be 1110), but the rest match. I'm not sure if that's an error in my algorithm, or error in input data (looking at the photo, I don't see where 0010 0100 appears).
Also note that samples provided are very similar (only bits 8,9,10,11 differ, if we exclude first sample), so there are probably many ways to end up with "a solution".

01 1011 - 11 1101 = ? use 2's complement

I'm trying to figure out this doozie: 01 1011 - 11 1101
use 2's complement to solve, 6bits signed.
This is what I tried:
range of 6bits: -32 to 31
01 1011 = 27
11 1101 = -29
27 -(-29) = 56 (overflow)
11 1101 -- 2s complement --> 10 0011
so
01 1011 + 10 0011 = (missing bit)11 1110 = -2!
Any luck?
Ok so I think I figured it out:
my first mistake was that 11 1101 is not -29 but -3.
SO:
01 1011 = 27
11 1101 = -3
27 -(-3) = 30
I can do a reverse 2's complement of -3 which is:
11 1101 - 1 = 11 1100
then I flip 11 1100 which gives me 00 0011.
so
01 1011 + 00 0011 = 01 1110 = 30
:)

MIPS: Calculating BEQ into Hexadecimal Machine Code

I have an assignment where I have to convert MIPS instructions into its hexadecimal machine code. I know how to convert the add, addi, lw, etc. instructions just fine, but when it gets to instructions like beq, I get confused. How would I convert this beq to hex?
0x00400108 beq $t3, $t5, NEXT
0x0040010C j END
where the address of NEXT is
0x0040011C
?
What I've tried:
beq opcode = 4
$t3 = register 11
$t5 = register 13
NEXT = 0x0040011C - 0x0040010C = 10 (hex) = 16 (decimal)
4 11 13 16 (decimal)
000100 01011 01101 0000 0000 0000 1000 (convert to binary)
0001 0001 0110 1101 0000 0000 0000 1000 (group into fours)
1 1 6 D 0 0 0 8 (hexadecimal)
but it's wrong...
After spending a long time being dumb, I've found the correct answer.
The summarized code:
beq $t3, $t5, NEXT
[instruction 1]
[instruction 2]
[instruction 3]
[instruction 4]
NEXT: [instruction 5]
As Michael said, the offset is the number of words from the instruction following the branch instruction. Instruction 1 is the following instruction after beq, so start counting from there till NEXT. There are 4 instructions from instruction 1 and NEXT, so the format for beq is now:
op | rs | rd | 16-bit constant or address
000100 | 01011 | 01101 | 0000 0000 0001 0000
Where rs is $t3 and rd is $t5.
Regrouped and converted into hex:
0001 | 0001 | 0110 | 1101 | 0000 0000 0001 0000
1 | 1 | 6 | D | 0 0 1 0
So the hexadecimal representation is 116D0010. Cheers.
Edit:Instructions are Words, 4 Instructions * 4 Bytes = 16 bytes
Since the instructions have a offset of 3 commands from where the pc put in 3 and not 4 into the offset. Thus the binary rep is 0001 | 0001 | 0110 | 1101 | 0000 0000 0000 0011 and not what the edited answer says about multiplying by 4.
Check out the example in this pdf: https://ai.berkeley.edu/~cs61c/sp17/lec/11/lec11.pdf

Change table structure using dynamic SQL and not knowing the number of attributes in SQL Server 2008

If I have a table like this (where I do not know the name of the columns and How many are there, in this example there are 5 attributes, with 20 rows:
x1 x2 x3 x4 x5
----------------------------
438 498 3625 3645 5000
438 498 3625 3648 5000
438 498 3625 3629 5000
437 501 3625 3626 5000
438 498 3626 3629 5000
439 498 3626 3629 5000
440 5000 3627 3628 5000
444 5021 3631 3634 5000
451 5025 3635 3639 5000
458 5022 3640 3644 5000
465 525 3646 3670 5000
473 533 3652 3676 5000
481 544 3658 3678 5000
484 544 3661 3665 5000
484 532 3669 3662 2945
482 520 3685 3664 2952
481 522 3682 3661 2955
480 525 3694 3664 2948
481 515 5018 3664 2956
479 5000 3696 3661 2953
How would you get something like this
(5 columns * 20 rows = 100 rows where every 20 rows we have a column of previous table)
Id Ordinal Name Value
----------------------------
1 1 x1 438
2 1 x1 438
3 1 x1 438
4 1 x1 437
5 1 x1 438
6 1 x1 439
7 1 x1 440
8 1 x1 444
9 1 x1 451
10 1 x1 458
11 1 x1 465
12 1 x1 473
13 1 x1 481
14 1 x1 484
15 1 x1 484
16 1 x1 482
17 1 x1 481
18 1 x1 480
19 1 x1 481
20 1 x1 479
1 2 x2 498
2 2 x2 498
3 2 x2 498
4 2 x2 501
5 2 x2 498
6 2 x2 498
7 2 x2 5000
8 2 x2 5021
9 2 x2 5025
10 2 x2 5022
11 2 x2 525
12 2 x2 533
13 2 x2 544
14 2 x2 544
15 2 x2 532
16 2 x2 520
17 2 x2 522
18 2 x2 525
19 2 x2 515
20 2 x2 5000
1 3 x3 3625
2 3 x3 3625
3 3 x3 3625
4 3 x3 3625
5 3 x3 3626
6 3 x3 3626
7 3 x3 3627
8 3 x3 3631
9 3 x3 3635
10 3 x3 3640
11 3 x3 3646
12 3 x3 3652
13 3 x3 3658
14 3 x3 3661
15 3 x3 3669
16 3 x3 3685
17 3 x3 3682
18 3 x3 3694
19 3 x3 5018
20 3 x3 3696
1 4 x4 3645
2 4 x4 3648
3 4 x4 3629
4 4 x4 3626
5 4 x4 3629
6 4 x4 3629
7 4 x4 3628
8 4 x4 3634
9 4 x4 3639
10 4 x4 3644
11 4 x4 3670
12 4 x4 3676
13 4 x4 3678
14 4 x4 3665
15 4 x4 3662
16 4 x4 3664
17 4 x4 3661
18 4 x4 3664
19 4 x4 3664
20 4 x4 3661
1 5 x5 5000
2 5 x5 5000
3 5 x5 5000
4 5 x5 5000
5 5 x5 5000
6 5 x5 5000
7 5 x5 5000
8 5 x5 5000
9 5 x5 5000
10 5 x5 5000
11 5 x5 5000
12 5 x5 5000
13 5 x5 5000
14 5 x5 5000
15 5 x5 2945
16 5 x5 2952
17 5 x5 2955
18 5 x5 2948
19 5 x5 2956
20 5 x5 2953
How would be the dynamic version of this? In the example I have 5 attributes, but Let´s say I do not know how many atrributes are there.
I'm posting this, even though I'm upvoting #Martin's solution. His is much more elegant (I've still to intuitively know when it's best to use PIVOT and UNPIVOT), but also more unforgiving, as the original tables' columns must be all the exact same data type and follow the "x####" naming convention.
Since I use more column substitution when building the query, I used + CHAR(13) + CHAR(10) to add line breaks to the
dynamic SQL, in an attempt to make the code (both before and after) more legible.
DECLARE
#TableName sysname
,#Command nvarchar(max)
SET #TableName = 'YourTable'
-- Build a big dynamic set of UNION SELECT statements
SELECT #Command = isnull(#Command + char(13) + char(10) + 'union all ', '')
+ 'select ' + cast(column_id as varchar(10)) + ' Ordinal, ''' + name + ''' name, ' + name + ' value ' + char(13) + char(10)
+ ' from ' + #TableName
from sys.columns
where object_id = object_id(#TableName)
-- Wrap the above as a subquery, to enable the use of row_number()
SET #Command = 'select row_number() over (partition by Ordinal order by Ordinal) Id, Ordinal, Name, Value' + char(13) + char(10)
+ ' from (' + #Command + ') bigUnion'
--PRINT #Command
EXECUTE (#Command)
(Addenda)
Inserting the results of a dynamically created query into a temp table is fairly easy, so long as you don't have to dynamically create the temp table.
Temp tables only last for as long as the “session” in which they are created. If created at the start of a stored procedure, they persist until that stored procedure (and any procedures it calls) is finished; if created within a dynamically created script, they persist until that script finishes. So you could build a “SELECT… INTO #…” dynamic script, teh table would be populated, and would then be dropped when that script finished execution. To persist the data longer than that, you’d have to create the temp table before calling the dynamic script, something like so:
CREATE TABLE #MyTempTable
(
Id
,Ordinal
,Name
,Value
)
assigning the appropriate data types and nullability, and then populating it with
INSERT #MyTempTable (Id, Ordinal, Name, Value)
EXECUTE (#Command)
DECLARE #DynSQL NVARCHAR(MAX)
SELECT #DynSQL = ISNULL(#DynSQL+ ',','') + QUOTENAME(name)
FROM sys.columns WHERE object_id=OBJECT_ID('dbo.YourTable')
SET #DynSQL = '
;WITH T AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS Id
FROM YourTable)
SELECT Id,
CAST(SUBSTRING(Name,2,10) AS INT) AS Ordinal,
Name,
Value
FROM T
UNPIVOT(Value for Name in (' + #DynSQL + ')) U
ORDER BY Ordinal,Id'
EXEC(#DynSQL)