@venezia is on PowPing!

PowPing is a place where you can earn Bitcoin simply by socializing, for FREE.
Never tried Bitcoin? It's OK! Just come, socialize, and earn Bitcoin.
Check out venezia's activities

Bitcoin Script

visit channel home
Total Economy: 0.02 USD
One thing that gave me a headache at first is that all the numbers are little endian. When you get used to it, you don't feel bothered anymore. Similar to other languages, Bitcoin provides these operators to manipulate bits. Can you implement the function of "is_negative" without using any comparison opcodes? It returns false when the input is a positive number, and true when it is a negative number. ("true" can be any value not equal to 0)
This is one of solutions: ``` dup if size 1- dup if 0 swap num2bin endif <<0x80>> cat & 1 rshift not0 else drop 0 endif ``` The first IF checks whether the input is 0, because 0 is the most special, it may be an empty string. If it is 0, it directly returns 0 (false). If the input is not 0, then all we need to do is get the sign bit. We can use the bitwise AND operation to get the bits. OP_ADD requires that the lengths of the two inputs are equal. So we padded zeros to the left of 0x80 to make it equal to our input length. Finally, we shift the sign bit to the right by one bit and use OP_0NOTEQUAL to determine its value.
Well, I know you said no comparison operators but this kinda works for both 8 and 32 bit numbers -- OP_SIZE OP_1 OP_GREATERTHAN OP_IF 1F OP_RSHIFT OP_ELSE 07 OP_RSHIFT OP_ENDIF
adonsats replied:
brute force!
venezia replied:
Awesome! It will be great If can handle arbitrary size integer.
not logged in replied:
Integer in bitcoin script is little endian, in other words, the sign bit is at the first bit of last bytes. For example, -256 big endian: 10000001 00000000 first 1 is sign bit. reverse the bytes, we got little endian: 00000000 10000001
venezia replied:
Wow, I logged out before commenting😂
light replied:
So does it need to be inverted, and then shifted? I've been trying to grok this but I'm not sure how to accomplish the goal yet, unfortunately... Yeah, the endian-ness does weird things when you study the output of the numbers... 256 comes out as 00 01 in script...
light replied:
Dividing by 8 and then right shifting by 7 works for some larger numbers but then not for smaller ones. I feel like I'm missing something big here with the bit manipulation techniques. :-)
adonsats replied:
bit manipulation is obscure code :) do not worry
"x" 07 OP_RSHIFT
venezia tipped:
0.01 USD
1 year ago
light replied:
OP_1NEGATE 07 OP_RSHIFT -- gives <Buffer 01> OP_1NEGATE OP_6 OP_SUB 07 OP_RSHIFT -- gives <Buffer 01> OP_1 07 OP_RSHIFT -- gives <Buffer 00> OP_7 07 OP_RSHIFT -- gives <Buffer 00>
light replied:
I realized that a -1 leaves 0x80 on the stack, and that is the highest bit in the 8 bit number being set. We shift the number to the right 7 bits and this leaves that "sign" (highest) bit in the 1st bit's place, and thus is either 0 or 1 depending on if the number was positive (not signed) or negative (signed).
light replied:
(I meant to say that -1 leaves 0x81 on the stack, but it's the 0x80 that we're most concerned about)
venezia replied:
Cool! It works well for 8bit numbers. How about larger numbers?
adonsats replied:
in deed.
ok. let us try. ( n -- n ) : is_negative 31 OP_RSHIFT OP_1 OP_AND ; really have some problem with hex, ascii, so correct if need 🤓
venezia tipped:
0.01 USD
1 year ago
light replied:
OP_1NEGATE 1F OP_RSHIFT OP_1 OP_AND -- That doesn't seem to work, unfortunately...
adonsats replied:
where is the problem?
light replied:
I was wrong about the shift by 32 bits. It's a right shift by 7 bits.
venezia replied:
Should works for 32bit big endian numbers. : ) In bitcoin script, all number is little endian.
adonsats replied:
hihi. the tip show an asnwer or close to the answer ? :) I have try 🤪. may be some explanation?
light replied:
I went back and tested your function in gforth and it's spot on.
adonsats tipped:
0.01 USD
1 year ago
venezia replied:
In most common languages integer is big endian, bitcoin is a little special.
adonsats replied:
why is that ?
I know this one is probably going to involve OP_RSHIFT and shifting bits by 31 but so far I can't figure out how to make that work properly. :-)