Mailing List Archive: 49091 messages

# Bitwise manipulation

### [1/3] from: philip:hayes:btcellnet at: 11-Oct-2000 16:08

An interesting one ... I have a integer value that I must convert to binary: Nice and easy for values <= 127 int1: 56 b1: to-binary to-block int1 HOWEVER, if int1 is greater than 127, say 2037, I have to perform the following task: 2037 which is equivalent to 0000100001011000 must be split into 2 bytes as follows b1: -> 00001000 effectively b1: to-binary to-block 8 b2: -> 01011000 effectively b2: to-binary to-block 24 Any suggestions.. Phil ********************************************************************** This email and any attachments may be confidential and the subject of legal professional privilege. Any disclosure, use, storage or copying of this email without the consent of the sender is strictly prohibited. Please notify the sender immediately if you are not the intended recipient and then delete the email from your inbox and do not disclose the contents to another person, use, copy or store the information in any medium. **********************************************************************

### [2/3] from: joel:neely:fedex at: 11-Oct-2000 10:50

Hi, Phil... [Philip--Hayes--btcellnet--net] wrote:
> An interesting one ... > I have a integer value that I must convert to binary:
<<quoted lines omitted: 8>>
> b2: -> 01011000 effectively b2: to-binary to-block 24 > Any suggestions..
Two suggestions: 1) Try using big-endian: func [n [integer!] /local r] [ r: copy #{} until [ insert r to-binary to-char (n // 256) 0 = n: to-integer n / 256 ] r ]
>> big-endian 56
== #{38}
>> big-endian 2037
== #{07F5} However, *DANGER, WILL ROBINSON!*, 1.1) This assumes byte order for values requiring more than 8 bits. 1.2) It breaks for negative values! (Explaining why and proposing a repair are left as exercises for the reader... ;-) 2) Check your math. Last time I looked, 2037 was odd, which means that its binary representation MUST end in a one! ;-) -jn- -- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] print to-string debase decompress #{ 789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}

### [3/3] from: mdb::gci::net at: 11-Oct-2000 15:13

What's wrong with
>> to-hex 56
== #00000038
>> to-hex 2037
== #000007F5
>> to-hex -56
== #FFFFFFC8
>> to-hex -2037
== #FFFFF80B Mike. ----- Original Message ----- From: [joel--neely--fedex--com] Date: Wednesday, October 11, 2000 7:50 am

Notes
• Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted