This may end up as a report on SSRS (MS SQL Server) but ultimately I need to come up with a way to determine what combination of 2 numbers (can be used multiple times) will fit inside 1 large number.

In my scenario I am dealing with total bitrate of video. we have 2 types of streams

- HD: 8.25Mbit/s (8250000bps)
- SD: 2.5Mbit/s (2500000bps)

And then we have a total bitrate allowed as 72Mbit/s (7200000bps). I would like to find out how many HDs or SD fit into 72Mbit/s.

With the total bit rate of 72MBit we would like to find out the best way to cram as many HD or SD streams into the total.

I am asking it as a general IT Programming question right now to figure out the algorithm, but ultimately I believe it will end up as an SQL report.

Edited Aug 14, 2014 at 18:40 UTCNot sure why Jshowa deleted all of his posts in this thread. Here is the script that was marked as Best Answer.

```
DECLARE @X INT; -- local variables inside stored
-- procedure/function
DECLARE @Y INT;
DECLARE @NewX FLOAT;
DECLARE @NewY FLOAT;
DECLARE @PrevDiff FLOAT;
DECLARE @CurrDiff FLOAT;
-- initialize variables
SET @X = 0;
SET @Y = 0;
SET @NewX = 0;
SET @NewY = 0;
SET @PrevDiff = 0;
SET @CurrDiff = 0;
-- E = Excess Bandwidth, H = HD Bit Rate, S = SD Bit Rate
-- Y = Number of HD streams, X = Number of SD streams
WHILE (@Y <= (@E / @H)) -- Fix Y
BEGIN
WHILE (@E >= @S * @X + @H * @Y) -- Calculate sum of bit rates with Y fixed
BEGIN
SET @CurrDiff = @E - (@S * @X + @H * @Y); -- Compare E with sum
IF (@CurrDiff < @PrevDiff)
BEGIN
SET @PrevDiff = @CurrDiff; -- retain comparison if smaller than previous
SET @NewX = @X; -- retain X and Y counts for this new smaller comparison
SET @NewY = @Y;
END
SET @X = @X + 1; -- Increment X and reloop with Y still fixed
END
SET @Y = @Y + 1; -- after iterating through X with Y fixed until E is exceeded
SET @X = 0; -- increment Y and reset X and then rerun comparisons again
END
-- You'll have to tailor the insert into the table to your needs,
-- but the HD/SD combination is added to a table here, at the end of the loops
INSERT INTO <HD and SD Info Table Name> <Bit Rate Allowance>
VALUES @NewY + 'HD' + '/' + @NewX + 'SD'
```

## 38 Replies

Howell IT is an IT service provider.

B is a byte and b is a bit.

8 bits is equal to 1 bytes

1024 bytes = 1 KB

1024KB = 1MB

And

1024 bits = 1kb

1024 kb = 1Mb

I believe you have your bits and bytes confused.

Edit: fixed the bit/byte confusion

Edited Aug 13, 2014 at 19:20 UTCB is a byte and b is a bit.

8 bytes is equal to 1 bit.

1024 bytes = 1 KB

1024KB = 1MB

And

1024 bits = 1kb

1024 kb = 1Mb

I believe you have your bits and bytes confused.

8 bits = 1 byte

The rest looks correct and matches what I've said.

1kB is not the same as 1kb. 1024 is the same but the units are different.

Howell IT is an IT service provider.

B is a byte and b is a bit.

8 bytes is equal to 1 bit.

1024 bytes = 1 KB

1024KB = 1MB

And

1024 bits = 1kb

1024 kb = 1Mb

I believe you have your bits and bytes confused.

8 bits = 1 byte

The rest looks correct and matches what I've said.

1kB is not the same as 1kb. 1024 is the same but the units are different.

Ah, yes! That's what I get for typing on a smartphone.

For bitrate, isn't 1 Mb actually 1,048,576 bits? (1024*1024)

1MB is but 1Mb is not. B=byte b=bit

Then 1Mb is actually 1,048,576 bits... 1MB is 8,388,608 bits.

B is a byte and b is a bit.

8 bytes is equal to 1 bit.

1024 bytes = 1 KB

1024KB = 1MB

And

1024 bits = 1kb

1024 kb = 1Mb

I believe you have your bits and bytes confused.

8 bits = 1 byte

The rest looks correct and matches what I've said.

1kB is not the same as 1kb. 1024 is the same but the units are different.

Ah, yes! That's what I get for typing on a smartphone.For bitrate, isn't 1 Mb actually 1,048,576 bits? (1024*1024)

1MB is but 1Mb is not. B=byte b=bit

Then 1Mb is actually 1,048,576 bits... 1MB is 8,388,608 bits.

Correct :)

Howell IT is an IT service provider.

Haha, you have me confused! I think we are arguing for the same thing. Lol, because that is what I said the first time.B is a byte and b is a bit.

8 bytes is equal to 1 bit.

1024 bytes = 1 KB

1024KB = 1MB

And

1024 bits = 1kb

1024 kb = 1Mb

I believe you have your bits and bytes confused.

8 bits = 1 byte

The rest looks correct and matches what I've said.

1kB is not the same as 1kb. 1024 is the same but the units are different.

Ah, yes! That's what I get for typing on a smartphone.For bitrate, isn't 1 Mb actually 1,048,576 bits? (1024*1024)

1MB is but 1Mb is not. B=byte b=bit

Then 1Mb is actually 1,048,576 bits... 1MB is 8,388,608 bits.Correct :)

Howell IT is an IT service provider.

The point I was trying to make was that bitrate is measured in binary rather than decimal. So 1Mb is 1,048,579 bits rather than 1,000,000 bits.

That is very relevant to the discussion.

Howell IT is an IT service provider.

Several sources I found agree with you, so I stand corrected.however we are talking bitrate and that is 1kbit/s = 1000b/s not 1024

Sorry to waste your time.Great 9 Posts to solve your discussion on bit & byte and no help whatsoever for the OP. Glad we settled that and can get back to helping him.

Basically that was what I was implying by the equations. One calculatesMy general solution is to fix X and run through all Y's up to the HD bit rate and then do the opposite for SD. That's the inelegant way, but I may be able to figure out another one.

using a fixed number of SD streams how many HD streams you could get

from the remaining or vice versa. Simple Algebra (Curses math teacher for being right) x+y=z. We know Z and can put a constant in for the one of the others so to speak.

Wasn't a matter of wasting my time, but rather trying to get the OP an answer to his problem. All good. Shreddie wrote:Sorry to waste your time.Great 9 Posts to solve your discussion on bit & byte and no help whatsoever for the OP. Glad we settled that and can get back to helping him.

If it only came up with one combination, then something is not quite right. You could get results like 8 HD with 2 SD, 7 HD with 5 SD, 6HD with 9SD, 5HD with 12 SD, 4HD with 15SD ......

The 6HD with 9SD might be the only one that completely fills the total bitrate, as the others will have leftovers...

So here is some real data that I'm working with to make this a little easier to explain.

In this example there are 20 servers.

The total Bitrate column should actually be the "Currently in use" column, and there should be another column for total which would be be 72000000 as described earlier, and then "Currently in use" is whatever is not provisioned. On the far right for Bit Rate allowance, it will not go above 2HD (because of the way the previous programmer created the report and hard set the numbers.

So to make this easier/or harder I want to use, what is in the "Total Bitrate" column and figure out how many more HD's or SD's will fit there, using the BEST combination to get as close to 72000000 (72MB) max as possible WITHOUT going over. So there should only be 1 result produced. As you can see with ASERV009 and ASERV014 they are "OVER LIMIT" because they are currently provisioned to use more than 72000000.

The only columns I have in a database to work with are Name, and Total Bitrate, the other 2 columns are calculations in SQL. I need to update these calculations to be a bit more accurate. As you can see with ASERVER005 although there is much more bandwidth available (see the excess bitrate column) than 2 HD. That is all there says is available. It should actually say like 6HD or 5HD and 1 or 2 SD (Whatever fits inside the mac bitrate)

This may be much harder than I think it is.

Hopefully this explains it a little better.