dividing by two in binary numbers - binary

how can I divide a binary number (in 2 complement) by 2 .
I've tried bit shifting but I have a problem with negative odd numbers, how can I solve it?
for example :
(-7/2) 11001/2 => (shifts the number one place to the right) => 11100 (-4)

Can you use after the . ? Like this?
1 0011 . 1000
Part 1 2 3 4
Part 1: Negative number
Part 2: Makes 3
Part 3: Separate by .
part 4: Makes 0.5
The negative part works like:
.1000 = 0.5
.0100 = 0.25
.1100 = 0.75
.0010 = 0.125
So instead of multiply by 2 to shift to left. You can do a divide by 2 to the right. So after the dot /2 and before the dot x2.
Hope you can use this information.

Related

Minimum expected length of a message

A bag contains 16 balls of following colors: 8 red, 4 blue, 2 green, 1 black and 1 white. Anisha picks a ball randomly from the bag and messages Babu its color using a string of zeros and ones. She replaces the ball in the bag and repeats this experiment many times. What is the minimum expected length of the message she has to convey to Babu per experiment?
(a)3/2
(b)log 5
(c)15/8
(d)31/16
(e)2
According to me, since the ball is taken out with replacement. At any time, there are 16 balls of 5 different colors in the bag. To encode 5 colors, ceiling of log5 (base 2) i.e. 3 bits should be needed but the answer given is (15/8). Can someone point out my mistake and provide some hint for the correct solution?
using static huffman compression you can encode the more common colours in fewer bits than the rare colours, that being the case on can expect that common colours will usually be chosen.
eg:
red 1
blue 01
green 001
white 0001
black 0000
on average from 16 draws there will be
8 reds = 8 bits
4 blues = 8 bits
2 greens = 6 bits
1 white = 4 bits
1 black = 4 bits
for a total of 30/16 bits on average
Your answer is right as the maximum value needed for encoding. But consider the following coding scheme 1 - red (1/2 prob), 01 - blue (1/4 prob), 00 - green (1/8 prob), 001 - black (1/16 prob), 000 - white (1/16 prob) multiply message length by probability and you should have 1 + 5/8 ( not 15/8 ... though)

actionscript Number add and substract strange behavior

Well i just got a problem, with the simple following code:
trace( 0.01+0.05 ); // 0.060000000000000005
trace( 0.03-0.01 ); // 0.019999999999999997
I mean i just want 0.01+0.05 give me 0.06 and 0.03-0.01 give me 0.02.
Does someone have an idea how to retrieve the correct results ?
The imprecision is due to floating point arithmetic. 0.01, 0.05 and 0.03 are all floating point literals. Not every number (in fact, very few numbers) can be represented precisely in floating point.
For example, 0.5 can be but, 0.06 cannot. As a rule of thumb the first 15 significant figures will be correct.
For more details, see http://en.wikipedia.org/wiki/Floating_point
trace(Math.format((0.01+0.05), 2));

Validate Mobile Number Pattern

I want to validate a mobile number...It starts with 0 and the next three digits pattern is 1 to 7 number and then a '-' between the next starting number with 7 digits starts with 0 to 9
The pattern I want to follow is that :
0333-1234567
0312-3342090
Try this:
pattern="[0][1-7]{3}[-][0-9]{7}"
Demo

Drawing a bar chart, but with some restriction

I want to draw a chart in linux like this:
1################# 64.85
2################### 72.84
3####################### 91.19
4####################### 91.61
5########################### 108.66
6############################ 110.69
7###################################### 149.85
8####################################### 156.60
9########################################### 169.81
I want to do that in python, of course you noticed that I don't want code like:
for i in data:
print "#"*i
because data may contain big numbers, so it is not nice to print "#" milion times.
So what is the mathematical equation that I must use to do this, I think this is a kind of mathematical problem
Thanks a lot
You have to work with percentages I think sum up all you values and then you do bar value / total of bar values
So if I have the following values 1 2 3 6 the total will be 12 so then
i will do 1 / 12 the percentage will be 8 so you print '#' 8 times and so on.
then the max # you can print is hundred.
I don't know if this is what you want, but hope this will help.

What Colour Format/Encoding are these Flash colour values in

I am parsing colour codes that I get from a Flex(Flash ActionScript) application, then creating HTML div elements with that same colour.
My Problem: The colours are all only 8 digits long. So they cant be RGB colour values can they? What color value format are they in? If I can figure the format they are in I can convert them to RGB. Maybe the last/first digit signifies its 0. alpha value?
PS: Should I convert the colours to RGB or something else?
This is an example of the colour code values I getting from the flash application:
16777215
4803910
84545883
16777215
RGB colours are represented by hexadecimal digits (base 16).
Base 16 means that each place in the number can represent numbers 0-15 in this order:
0 1 2 3 4 5 6 7 8 9 A B C D E F
Using 0x in AS3 represents a hex number. As an example, run this:
trace(0xF); // 15
The output as you can see is represented in decimal (base 10). What you're seeing above in your question is the decimal representation of your colours.
If you want to see the hex version, use toString() and parse 16 as the radix parameter. You'll notice that the default is 10 (for base 10 / decimal numbers that we all know and love).
var num:int = 15;
trace(num.toString(16)); // f
Hope this makes sense.