I have 5 bits and so 32 different combinations (of them).
Starting from
00000
and ending with
11111
Is there some way of quickly listing all possibilities? I could do it by hand, but I worry that I might miss one. I'm guessing some clever chap has written some algorithm and/or made a website, that can do this very easily. At least I hope so.
Thanks a lot.
This will put them all on the command line on Linux.
echo {0..1}{0..1}{0..1}{0..1}{0..1}
In Ruby:
0b0000.upto(0b1111) {|n| puts n.to_s(2).rjust(4,"0")}
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Write a column with integer from 0 to 31, then write a second column with the binary equivalent of each integer side-by-side.
That way you will increase your chance not to miss a combination.
Just count from 0 to 31 and output the digit in it's binary form.
Something like this should do:
public static String zeroPad(String str) {
return "00000".substring(str.length()) + str;
}
public static void main(String[] args) {
for (int i = 0; i < 32; i++)
System.out.printf("%s%n", zeroPad(Integer.toBinaryString(i)));
}
Output:
00000
00001
00010
00011
...
11110
11111
for (int i = 0; i <31; i++)
cout << ((i & 16) >> 4) << ((i & 8) >> 3) << ((i & 4) >> 2)
<< ((i & 2) >> 1) << (i & 1) << endl;
Unix:
echo {0..1}{0..1}{0..1}{0..1} | xargs -n 1
Related
Operations on bits.
How to take 2 bits from byte like this:
take first 2 from 12345678 = 12;
Make new byte = 00000012
For example as asked in discussion by jspit :
$char = 'z'; //is 122, 0111 1010
$b = $char & '?'; // ? is 63, 0011 1111
echo $b; //$b becomes 58 and shows ':'
//if integer used you get:
$b = $char & 63;// 63 is 0011 1111 as '?' but $char is string and you get 0 result:
echo $b; //$b becomes 0 because conversion to integer is used from string and $char becomes 0 and get 0 & 63 = 0, and here is error.
For clearance operation is on bits not on bytes, but bits from bytes.
'string' >> 1 not work, but this is second problem.
Codes of char You can check on my site generating safe readable tokens, with byte template option on. Site is in all available languages.
I think I found good answer here:
how to bitwise shift a string in php?
PS. Sorry I cant vote yours fine answers but I have no points reputation here to do this ;)...
I hope you understand bits can only be 0 or 1, I'm assuming when you say "12345678" you're just using those decimal symbols to represent the positions of each bit. If that is the case, then you're looking for bitwise operators.
More specifically:
$new = $old >> 6;
This bitwise shift operation will shift all bits 6 positions to the right, discarding the 6 bits that were there before.
You can also use an or operation with a bitmask to ensure only 2 bits remain, in case the variable had more than 8 bits set:
$new = ($old >> 6) | 0b00000011;
function highestBitsOfByte(int $byte, int $count = 2):int {
if($count < 0 OR $count > 8) return false; //Error
return ($byte & 0xFF) >> (8-$count);
}
$input = 0b10011110;
$r = highestBitsOfByte($input,2);
echo sprintf('%08b',$r);
The integer number is limited to the lowest 8 bits with & 0xFF. Then the bits are shifted to the right according to the desired length.
example to try: https://3v4l.org/1lAvO
If there is a character as input and the fixed number of 2 bits is required, then this can be used:
$chr = 'z'; //0111 1010
$hBits = ord($chr) >> 6;
echo sprintf('%08b',$hBits); //00000001
Can you please explain how this line of code is equivalent to the next code:
<?php
$string = chr( ( $number >> 6 ) + 192 ).chr( ( $number & 63 ) + 128 );
?>
Its equivalent to :
if ( $number >=128 && $number <=2047 ){
$byte1 = 192 + (int)($number / 64); //= 192 + ( $number >> 6 )
$byte2 = 128 + ($number % 64); //= 128 + ( $number & 63 )
$utf = chr($byte1).chr($byte2);
}
for example entering number 1989 both produces ߅
These codes are used for converting UNICODE Entities back to original UTF-8 characters.
The code on top uses binary operators.
>> is right shift operator. It shifts the bit in the number to the right (towards more significant bits).
So 11110000 >> 2 = 00111100
It's equivalent to division by powers of 2
$number >> $n is the same as $number / pow(2,$n).
The & is the "bitwise and" operator. It compares respective bits on both numbers, and sets in result those, that are 1 in both numbers.
11110000 & 01010101 = 01010000
By and'ing $number with 63 (001111111) you get the remainder of dividing $number by 64 (aka the modulus), which is written $number % 64.
$number >> 6 is a binary shift-right operation, ie: 11000000 >> 6 == 00000011 equivalent to $number / pow(2,6) aka $number / 64
$number & 63 is a binary AND with 00111111
Both are much faster to do as binary operations since they deal with powers or two.
Adding to #Mchl's answer the reason for adding 192 in UTF sequence is to signal the start of byte information
192 - 11000000 - Start of 2 Byte sequence ( 128 + 64)
224 - 11100000 - Start of 3 Byte sequence ( 128 + 64 + 32)
240 - 11110000 - Start of 4 Byte sequence ( 128 + 64 + 32 + 16)
248 - 11111000 - Start of 5 Byte sequence (Restricted) (... + 8)
252 - 11111100 - Start of 6 Byte sequence (Restricted) (... + 4)
254 - 11111110 - Invalid
Table Reference : https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=388157043
UTF-8 byte range table
I'm experiencing different outputs in PHP code running in Mac and Linux.
I have 2 servers running the following code:
$ltt = ((ord($str[7]) << 24) | (ord($str[8]) << 16) | (ord($str[9]) << 8) | (ord($str[10]))) / 1000000;
Even the ord(str[ ]) outputs are the same:
[7] = 254
[8] = 26
[9] = 22
[10] = 216
But, on the MAMP stack (Mac) running php 5.3.6, if $ltt is originally supposed to be a negative number, it returns 4263.12265 (incorrect).
On the LAMP stack (Ubuntu) running same php version, it will return the exact negative value -31.84465.
This happens only with negative numbers..
Update Addl. Info:
A var dump gives þØçï_Kstring(25) "þØçï_K"
bin2hex gives 000e1b00000000fe1a16d806e707ef0000045f0000004b0000
Simplying the function to include only numeric inputs, the output still differs:
$ltt = (254 << 24 | 26 << 16 | 22 << 8 | 216)/ 1000000;
4263.12265 on MAMP and -31.84465 on LAMP
This is a 32 vs 64 bit problem.
Because your most significant byte is > 127, on a 32 bit platform this is interpreted as a negative value because of integer overflow - the most significant bit is set. On a 64-bit platform it is not.
The solution is to use pack() and unpack() so you can specify that the integer should be signed. EDIT Fixed this code sample See edit 2
$packed = pack('C*', ord($str[7]), ord($str[8]), ord($str[9]), ord($str[10]));
$unpacked = unpack('l', $packed);
$lat = current($unpacked);
...however you should also be conscious that this will not work on a little-endian architecture, because the byte ordering will be wrong. You can simply reverse the order of the packed bytes to work around this, but I am just trying to wrap my head around a works-everywhere solution.
EDIT 2
OK, it took me a while to wrap my head around this but we got there in the end:
What you need to do is, if the most significant bit is set, OR the result with a number where the least significant 32 bits are not set but the rest are. So the following works on both 32 and 64 bit:
<?php
// The input bytes as ints
$bytes = array(254, 26, 22, 216);
// The operand to OR with at the end
$op = $bytes[0] & 0x80 ? (~0 << 16) << 16 : 0;
// Do the bitwise thang
$lat = (($bytes[0] << 24) | ($bytes[1] << 16) | ($bytes[2] << 8) | $bytes[3]) | $op;
// Convert to float for latitude
$lat /= 1000000;
echo $lat;
Hey all so I've ran into a bit of a problem, from PHP I have to read some data from a binary file where SPACE is of the utmost importance so they've used 24 bit integers in places.
Now for most of the data I can read with unpack however pack/unpack does not support 24 bit int's :s
I thought I could perhaps simple read the data (say for example 000104) as H* and have it read into a var that would be correct.
// example binary data say I had the following 3 bytes in a binary file
// 0x00, 0x01, 0x04
$buffer = unpack("H*", $data);
// this should equate to 260 in base 10 however unpacking as H* will not get
// this value.
// now we can't unpack as N as it requires 0x4 bytes of data and n being a 16 bit int
// is too short.
Has anyone had to deal with this before? Any solutions? advice?
If the file has only 3 bytes as above, the easiest way is padding as #DaveRandom said. But if it's a long file this method becomes inefficient.
In this case you can read each element as a char and a short, after that repack it by bitwise operators.
Or you can read along 12 bytes as 3 longs and then splits it into 4 groups of 3 bytes with bitwise operators. The remaining bytes will be extracted by the above 2 methods. This will be the fastest solution on large data.
unsigned int i, j;
unsigned int dataOut[SIZE];
for (i = 0, j = 0; j < size; i += 4, j += 3)
{
dataOut[i] = dataIn[j] >> 8;
dataOut[i + 1] = ((dataIn[j] & 0xff) << 16) | (dataIn[j + 1] >> 16);
dataOut[i + 2] = ((dataIn[j + 1] & 0xffff) << 8) | (dataIn[j + 2] >> 24);
dataOut[i + 3] = dataIn[j + 2] & 0xffffff;
}
The following question has an example code to unpack a string of 24/48bits too
I basically need to port this piece of code to php
for (i = 0; i < 128/4; i++)
data32[i] = bswap_32(data32[i]);
But, there is no bswap function in php.
Would someone be kind enough to provide me with something that could solve the problem?
This should do it (untested):
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (($j & 0xff000000) >> 24);
}
Or, if there is a sign extension problem, this should resolve it:
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (255 & (($j & 0xff000000) >> 24));
}
It sounds like bswap_32 is swapping endianness of your 32-bit quantities.
I could just give you some code, but I'd prefer not to do people's work for them, so I'll explain the principle instead:
You can achieve that with bit-shifts and masks (so for instance, you need to mask out the 8 lowest bits, and shift them into the highest 8 bit positions of the result).
Shifting can be done with the << and >> operators. Masking can be done with the & operator. See the PHP manual page on operators for more details.