Home » Community » Forums » » Bitwise Operations in C
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Bitwise Operations in C
Post Reply 
Nice article... I like boolean maths... I think people need to use it more often... even for pointless stuff... so I wrote this (VB) code which will add two bytes together without using the standard(ish) operators... and VB doesn't have a bitshift, and while I was going to settle with doubling the number, I figured that recoding it without it would be much better :

(and I left out comments for a reason... I call it the "verbosity challenge" - do as little as possible with as much as possible, and obscure what it is doing while you are at it )

Public Function Add(ByVal Byte1 As Byte, ByVal Byte2 As Byte) As Byte
    Dim Result As Byte
    Dim Temp As Byte
    Dim Temp2 As Byte
    Result = Byte1
    Temp = Byte2
    Do While Temp
        Temp2 = Result Xor Temp
        Temp = Result And Temp
        Result = Temp2
        If (Temp And 128) = 128 Then
            'Overflow
            Error 6
        End If
        If (Temp And 64) = 64 Then
            Temp = Temp Or 128
        Else
            Temp = Temp And (1 Or 2 Or 4 Or 8 Or 16 Or 32 Or 64)
        End If
        If (Temp And 32) = 32 Then
            Temp = Temp Or 64
        Else
            Temp = Temp And (1 Or 2 Or 4 Or 8 Or 16 Or 32 Or 128)
        End If
        If (Temp And 16) = 16 Then
            Temp = Temp Or 32
        Else
            Temp = Temp And (1 Or 2 Or 4 Or 8 Or 16 Or 64 Or 128)
        End If
        If (Temp And 8) = 8 Then
            Temp = Temp Or 16
        Else
            Temp = Temp And (1 Or 2 Or 4 Or 8 Or 32 Or 64 Or 128)
        End If
        If (Temp And 4) = 4 Then
            Temp = Temp Or 8
        Else
            Temp = Temp And (1 Or 2 Or 4 Or 16 Or 32 Or 64 Or 128)
        End If
        If (Temp And 2) = 2 Then
            Temp = Temp Or 4
        Else
            Temp = Temp And (1 Or 2 Or 8 Or 16 Or 32 Or 64 Or 128)
        End If
        If (Temp And 1) = 1 Then
            Temp = Temp Or 2
        Else
            Temp = Temp And (1 Or 4 Or 8 Or 16 Or 32 Or 64 Or 128)
        End If
        Temp = Temp And (2 Or 4 Or 8 Or 16 Or 32 Or 64 Or 128)
    Loop
    
    Add = Result
End Function
  


Trying is the first step towards failure.

Edited by - ragonastick on October 31, 2001 11:13:56 PM

 User Rating: 1023   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

lol

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: