Thread: Sign/unsign value based on data type length (bytes)

Results 1 to 7 of 7
  1. #1 Sign/unsign value based on data type length (bytes) 
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Code:
    	public static long unsign(long value, int amount)
    	{
    		// 1 = byte, 2 = short, 3 = tribyte, 4 = int, 8 = long (overflow)
    		return value &~ (0xFFFFFFFFFFFFFFFFL << (64 - (8 * (8 - amount))));
    	}
    	
    	public static long sign(long value, int amount)
    	{
    		long unsigned = unsign(-1, amount);
    		long signed = (unsigned & (0x7FFFFFFFFFFFFFFFL) << 1) >> 1;
    		
    		if (value > signed)
    		{
    			return value - (unsigned + 1);
    		}
    		
    		return value;
    	}
    Just thought it was an interesting idea. I got the idea of signing from the stream class in the client. Any better method to sign something unsigned?
    .
    Reply With Quote  
     

  2. #2  
    ¯̿ ̿|̿ ̿ |̶ ̶ ̶ ̶| |̶͇̿ ̶͇̿ ͇̿ Haskelle

    Join Date
    Nov 2007
    Age
    29
    Posts
    1,227
    Thanks given
    329
    Thanks received
    517
    Rep Power
    1133
    euhm... what????? O_O

    Just use two's complement? it's what is actually used to represent sign. (https://en.wikipedia.org/wiki/Two%27s_complement)


    it's basically

    Code:
    negative = ~positive + 1
    Code:
    byte a = 1 //0b0000_0001
    
    byte b = ~a;  // 0b1111_1110
    byte c = b+1; // 0b1111_1111 = 0xFF  = -1
    Monads are just Monoids in the category of Endofunctors. What is the problem?
    Costate Comonad Coalgebra is equivalent of Java's member variable update technology for haskell. Problem?
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Quote Originally Posted by Cups View Post
    euhm... what????? O_O

    Just use two's complement? it's what is actually used to represent sign. (https://en.wikipedia.org/wiki/Two%27s_complement)


    it's basically

    Code:
    negative = ~positive + 1
    Code:
    byte a = 1 //0b0000_0001
    
    byte b = ~a;  // 0b1111_1110
    byte c = b+1; // 0b1111_1111 = 0xFF  = -1
    i know. i want to get the unsigned value for a signed integer. not remove the sign bit.

    result for your "unsign" = 1337
    result for my unsign = 64199
    result for my resign (after unsign) = -1337

    if you would have actually run the code you would have noticed what it does. partially my fault for not creating a better title.
    .
    Reply With Quote  
     

  5. #4  
    ¯̿ ̿|̿ ̿ |̶ ̶ ̶ ̶| |̶͇̿ ̶͇̿ ͇̿ Haskelle

    Join Date
    Nov 2007
    Age
    29
    Posts
    1,227
    Thanks given
    329
    Thanks received
    517
    Rep Power
    1133
    Quote Originally Posted by Supah Fly View Post
    i know. i want to get the unsigned value for a signed integer. not remove the sign bit.

    result for your "unsign" = 1337
    result for my unsign = 64199
    result for my resign (after unsign) = -1337

    if you would have actually run the code you would have noticed what it does. partially my fault for not creating a better title.

    but our current representation of integers doesn't use a sign bit to represent negative numbers... so I have no idea what you're trying to accomplish.

    Why would you want to remove the 'sign bit'?
    Monads are just Monoids in the category of Endofunctors. What is the problem?
    Costate Comonad Coalgebra is equivalent of Java's member variable update technology for haskell. Problem?
    Reply With Quote  
     

  6. #5  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    So you transform an int to a long and retain it's decimal value through a lot more math than needed?
    Reply With Quote  
     

  7. #6  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Quote Originally Posted by Cups View Post
    but our current representation of integers doesn't use a sign bit to represent negative numbers... so I have no idea what you're trying to accomplish.

    Why would you want to remove the 'sign bit'?
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    note: no unsigned data types

    not sure what makes you so confused. it's simple. for any given amount of bytes (1 = byte, 2 = short, 4 = int, etc.) you can get the unsigned integer value for any negative number. the reason i am using a long is because it's the widest native type. if i want an unsigned byte i need to use a short, an unsigned short an int, an unsigned int a long, and an unsigned long isn't possible (with this implementation - inb4 link to biginteger). long encompasses the range of types i need and therefore is the logical choice to use.
    .
    Reply With Quote  
     

  8. #7  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    char is unsigned
    Reply With Quote  
     

  9. Thankful users:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 0
    Last Post: 01-06-2013, 12:58 AM
  2. Login player property bytes (9 bytes + 1 short)
    By Maxi in forum RS 503+ Client & Server
    Replies: 6
    Last Post: 03-22-2010, 09:16 PM
  3. Adding dueling sign(like wildy sign)
    By Richie in forum Help
    Replies: 2
    Last Post: 03-16-2010, 09:01 AM
  4. wild zones based on type
    By rmb7 in forum Help
    Replies: 2
    Last Post: 04-13-2009, 10:46 PM
  5. Replies: 15
    Last Post: 12-12-2008, 01:37 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •